简体   繁体   English

dllimport和MS Visual C ++中的模板

[英]dllimport and templates in MS Visual C++

I have a class like this 我有这样的课

class A_DLL A
{
   ...
   template <class T> someFunction(const T &v);
}

in library a.dll. 在库a.dll中。 A_DLL is __declspec(dllexport) when building a.dll and __declspec(dllimport) when using a.dll. 生成a.dll时A_DLL是__declspec(dllexport) __declspec(dllimport) ,使用a.dll时是__declspec(dllimport)

The problem is when I try to use 'someFunction' in some executable module linked against a.dll it works. 问题是当我尝试在与a.dll链接的某些可执行模块中使用'someFunction'时,它会起作用。 However when I use it in some other library (b.dll) it gives me an error message about unresolved externals (someFunction and other templates). 但是,当我在其他库(b.dll)中使用它时,它给我有关未解析的外部组件(someFunction和其他模板)的错误消息。 Obviously I should not use __declspec on templates but how then to make a class with __declspec ? 很显然,我不应该使用__declspec上的模板,但如何再使一类__declspec

You can use the declaration like: 您可以像这样使用声明:

template class A_DLL MyTemplateClass<int>;

This statement will generate the template class instantiation and export it in the DLL you are building (or import it depending on how A_DLL is set from preprocessor). 该语句将生成模板类实例化,并将其导出到您正在构建的DLL中(或将其导入,具体取决于从预处理器设置A_DLL的方式)。

You could also follow this link for a more detailed description. 您也可以点击此链接以获得更详细的描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM