代码:
#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/