Question on DLL Exporting/Importing and Extern on Windows

前端 未结 2 1667
情歌与酒
情歌与酒 2021-01-18 14:57

I have some quick questions on windows dll.

Basically I am using the ifdefs to handle the dllexport and dllimport, my question is actually regarding the placement of

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-18 15:36

    First, you don't need to import or export typedefs. As long as they're in the header files that both sides use, you're good. You do need to import/export functions and class definitions.

    Presumably you use the same header files for both the importing and exporting code, so you could do some makefile magic to define a preprocessor macro on each side, then do something like this:

    #if defined( LIBRARY_CODE )
    #define MYAPI __declspec(dllexport)
    #else
    #define MYAPI __declspec(dllimport)
    #endif
    
    extern MYAPI void func1();
    class MYAPI MyClass {
        ...
    };
    

    Regarding C vs. C++ functions, you can do this:

    #if defined( __cplusplus__ ) // always defined by C++ compilers, never by C
    #define _croutine "C"
    #else
    #define _croutine
    #endif
    
    extern _croutine void function_with_c_linkage();
    

    Make sure you import this header file from your C++ source file (containing the implementation of this function) or the compiler won't know to give it C linkage.

提交回复
热议问题