C++ 我可以这样混合pimpl和C接口吗?

C++ 我可以这样混合pimpl和C接口吗?,c++,c,api,interface,C++,C,Api,Interface,我想保持我的dll的向后兼容性,虽然Pimpl模式涉及必须导出整个类,因此名称损坏导致不同的编译器无法支持,在这种情况下,我可以像下面一样提供C兼容的接口吗 在公众头脑中: #ifdef CPPDYNAMICLINKLIBRARY_EXPORTS # define SYMBOL_DECLSPEC __declspec(dllexport) # define SYMBOL_DEF #else # define SYMBOL_DECLSPEC __declspec(dllimport)

我想保持我的dll的向后兼容性,虽然Pimpl模式涉及必须导出整个类,因此名称损坏导致不同的编译器无法支持,在这种情况下,我可以像下面一样提供C兼容的接口吗

在公众头脑中:

#ifdef CPPDYNAMICLINKLIBRARY_EXPORTS
#   define SYMBOL_DECLSPEC __declspec(dllexport)
#   define SYMBOL_DEF
#else
#   define SYMBOL_DECLSPEC __declspec(dllimport)
#   define SYMBOL_DEF      __declspec(dllimport)
#endif

#ifdef _WIN32
   #define GRAPHICAPI __stdcall
#else
   #define GRAPHICAPI
#endif 

#ifdef __cplusplus
   #define EXTERN_C     extern "C"
#else
   #define EXTERN_C
#endif // __cplusplus

#ifdef __cplusplus
namespace myapi{
  struct SYMBOL_DECLSPEC  Graphics{
        Graphics();
       ~Graphics();
       void drawLine(int,int,int,int);
       void drawLine(Point,Point);
  private:
     struct Impl;
     const Impl* impl;
  }
}//end of myapi   
#endif // __cplusplus

struct Graphics;
typedef struct Graphics *PGraphics;

#ifdef __cplusplus
  extern "C" {
#endif
  SYMBOL_DEF  PGraphics GRAPHICAPI newGraphics();
  SYMBOL_DEF void GRAPHICAPI deleteGraphics(PGraphics);
  SYMBOL_DEF  int GRAPHICAPI Graphics_drawLine4(PGraphics,int,int,int,int);
  SYMBOL_DEF  int GRAPHICAPI Graphics_drawLine2(PGraphics,Point,Point);
#ifdef __cplusplus
  }
#endif 
在dll项目中,def文件还有以下定义:

exports
         newGraphics    @1
         deleteGraphics    @2
         Graphics_drawLine4    @3
         Graphics_drawLine2   @4

如果未在def文件中指定序号,则在添加Graphics_drawArc等新函数时,函数Graphics_drawArc将在Graphics_drawLine4之前导出,而旧的应用程序调用Graphics_drawLine4确实调用了Graphics_drawArc,这实际上会导致崩溃


上述解决方案正确吗?

我会将调用约定隐藏在宏后面,以防您将其移植到另一个没有
概念的平台:

#ifdef _WIN32
#define GRAPHICAPI __stdcall
#else
#define GRAPHICAPI
#endif

SYMBOL_DEF  PGraphics GRAPHICAPI newGraphics();
SYMBOL_DEF void GRAPHICAPI deleteGraphics(PGraphics);
SYMBOL_DEF  int GRAPHICAPI Graphics_drawLine4(PGraphics,int,int,int,int);
SYMBOL_DEF  int GRAPHICAPI Graphics_drawLine2(PGraphics,Point,Point);

除此之外,我看它没有任何问题。不同的C++编译器之间的ABI差异会隐藏在C接口后面,而C接口具有相当稳定的ABI。

你面临的问题是什么?