c++ - __declspec(dllexport) 嵌套类

标签 c++ visual-c++ declspec

代码:

#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 类?

最佳答案

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

如果那个 B 类也被导出/导入(大概是),那么:是的,你做。

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

#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 MY_API B { // Add attribute to nested class
    //...

关于c++ - __declspec(dllexport) 嵌套类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69814706/

相关文章:

c++ - CFLAGS=-std=c++11 -O3 -Wall -pedantic 在 makefile 中未被识别

c++ - 计算字符串中的字符

c++ - 为什么我的线程不能正常退出?

c++ - 构建新库时包含库中的头文件

c++ - C/C++ 链接约定

c++ - OpenCV C++ - Windows IDE

c++ - 在 C++ 中转换 big-endian long?

我可以告诉 MSVC 编译器不要使用某个寄存器吗?

java - 执行在 "Microsoft Visual C++ Runtime Error"-popup 时停止

C - __declspec(thread) 变量性能