__declspec(dllexport) 嵌套 类

__declspec(dllexport) on nested classes

代码:

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

class MY_API A
{
    public:
        void some_method();

    class B
    {
         public:
             void other_method();
    };
};

我是否必须将我的宏 (MY_API) 添加到 B class?

Do I have to add my macro (MY_API) to the B class?

如果 B class 也是 exported/imported(大概是),那么:是的,你是。

尝试以下代码,我们在其中构建 DLL 并导出 classes:

#define BUILD_DLL

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

class MY_API A {
public:
    void some_method();

    class B {
    public:
        void other_method();
    };
};

// Dummy definitions of the exported member functions:
void MY_API A::some_method() {}
void MY_API A::B::other_method() {}

编译它会出现以下错误(MSVC,Visual Studio 2019):

error C2375: 'A::B::other_method': redefinition; different linkage

消息消失,代码编译没有问题,如果我们简单地将 MY_APP 属性添加到嵌套的 class:

//...
    class MY_API B { // Add attribute to nested class
    //...