Same header file for DLL and Static Library

Thus, a general (at least VS 2005) way to define export / import for a DLL:

#ifdef MY_EXPORTS
#define MY_API __declspec(dllexport)
#else
#define MY_API __declspec(dllimport)
#endif

class MY_API MyClass {
    ...
};

This works fine if I just create my code as a DLL. However, I want to be able to use a static library or DLL. Now one obvious (but terrible) solution is to copy all the code by deleting the DLL 'MY_API'. Now, a seemingly much better approach is to switch the command line to determine whether or not to define the material of the DLL. However, in the case of a static library, what should be "MY_API"?

#ifdef DLL_CONFIG
    #ifdef MY_EXPORTS
    #define MY_API __declspec(dllexport)
    #else
    #define MY_API __declspec(dllimport)
    #endif
#else
    #define MY_API // What goes here?
#endif

class MY_API MyClass {
    ...
};

Now, assuming this can be done, will there be problems when the library user includes header files (ie they must define "DLL_CONFIG")?

+5
3

.

#define MY_API, MY_API .

, Debug - DLL Release - DLL, , #define DLL_CONFIG.

, (, Debug/Release), " " . "Debug - DLL" Copy Settings Debug, , DLL_CONFIG.

→ → C/++ → DLL_CONFIG . , , NDEBUG WIN32.

haffax , . - :

#ifdef THEPROJECT_USE_DLL
    #ifdef THEPROJECT_BUILDING_PROJECT
        #define THEPROJECT_API __declspec(dllexport)
    #else
        #define THEPROJECT_API __declspec(dllimport)
    #endif
#else
    #define THEPROJECT_API
#endif

DLL #define THEPROJECT_USE_DLL, DLL, , "DLL".

+12

MY_API . :

#ifdef DLL_CONFIG
    #ifdef MY_EXPORTS
    #define MY_API __declspec(dllexport)
    #else
    #define MY_API __declspec(dllimport)
    #endif
#else
    #define MY_API
#endif

declspec.

DLL_CONFIG, dll , . . .

: , MY_EXPORTS DLL_CONFIG . , .

+3

. . , , - , .lib.

+1

All Articles