Exporting static data in a DLL

前端 未结 3 1141
不知归路
不知归路 2020-12-01 11:59

I have a DLL which contains a class with static members. I use __declspec(dllexport) in order to make use of this class\'s methods

3条回答
  •  我在风中等你
    2020-12-01 12:34

    My guess is that the class which uses the DLL should see dllimport instead of dllexport in the header. If I am correct, this can typically be achieved by defining a preprocessor macro like:

    #ifdef EXPORTING
    #define DECLSPEC __declspec(dllexport)
    #else
    #define DECLSPEC __declspec(dllimport)
    #endif
    

    and then use it in the class declaration:

    class DECLSPEC Test{
    protected:
        static int d;
    public:
        static void m(){}
    }
    

    So that in Test.cpp (or wherever it makes sense in your DLL project) you can specify that you are exporting so that it will be exported with dllexport:

    #define EXPORTING
    #include "Test.h"
    
    int Test::d;
    

    while the other project, which does not define EXPORTING, will see dllimport.

    Does it make sense?

提交回复
热议问题