【发布时间】:2015-01-21 08:19:40
【问题描述】:
我有一个第三方 dll,其中包含一个具有多个专业化的模板类。我在 Linux 上有自己的专长,尝试编译 windows dll 但会导致链接器错误。
我尝试了一下,发现模板头上的 dllimport 规范可能是原因,删除它可以解决我的问题。但是我不想修改或复制标头,因为它可能会随着第三方库的每次更新而中断。
这是重现我的问题的最小示例
test.h - dll/so 头文件:
#ifdef _WIN32
#ifdef EXPORT
# define LIB_EXPORT __declspec(dllexport)
#else
# define LIB_EXPORT __declspec(dllimport)
#endif
#else
# define LIB_EXPORT
#endif
template <typename A> class LIB_EXPORT Foobar
{
public:
virtual void helloWorld(){}
virtual ~Foobar(){}
};
test.cpp - dll/so impl:
#define EXPORT
#include "test.h"
template class __declspec(dllexport) Foobar<int>;
main.cpp - 示例程序:
#include "test.h"
//explicit instanciation - does not help
template class __declspec(dllexport) Foobar<char>;
int main(int argc, char** argv)
{
Foobar<char> a;
a.helloWorld();
}
有没有一种干净的方法可以在我的可执行文件中获得 Foobar 的完整实例化?
使用的编译器:Visual Studio 2010,g++ mingw w64 4.9.1
【问题讨论】:
标签: c++ visual-studio-2010 templates dll dllimport