假设我有这两个文件:
头文件.h
class DLL ExportClass{
public:
ExportClass();
static int test;
};
源.cpp
#ifdef EXPORT
#define DLL __declspec(dllexport)
#else
#define DLL __declspec(dllimport)
#endif
#include "Header.h"
int ExportClass::test = 0;
ExportClass::ExportClass(){
}
而且我不会定义EXPORT
(使用成员导入已经导出的类static
),为什么会收到这些警告:
1>source.cpp(11): warning C4273: 'test' : inconsistent dll linkage
1> header.h(4) : see previous definition of 'public: static int ExportClass::test'
1>source.cpp(13): warning C4273: 'ExportClass::ExportClass' : inconsistent dll linkage
1> header.h(3) : see previous definition of '{ctor}'
而这个错误:
1>source.cpp(11): error C2491: 'ExportClass::test' : definition of dllimport static data member not allowed
如果我定义EXPORT
它有效。我有点理解这些警告,但我认为编译器可以忽略静态变量和 ctor,因为整个类都被声明为__declspec(dllimport)
无论如何。我想对__declspec(dllexport)
and使用相同的代码库__declspec(dllimport)
- 但似乎编译器 stll 试图定义这些__declspec(dllexport)
在其声明中标记为的符号。解决这个问题的常见做法是什么?