【发布时间】:2013-11-12 10:32:40
【问题描述】:
假设我有这两个文件:
Header.h
class DLL ExportClass{
public:
ExportClass();
static int test;
};
Source.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) 和__declspec(dllimport) 使用相同的代码库——但编译器似乎试图定义这些在声明中标记为__declspec(dllexport) 的符号。解决这个问题的常见做法是什么?
【问题讨论】:
标签: visual-c++ dllimport dllexport