简体   繁体   中英

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 is __declspec(dllexport) when building a.dll and __declspec(dllimport) when using a.dll.

The problem is when I try to use 'someFunction' in some executable module linked against a.dll it works. However when I use it in some other library (b.dll) it gives me an error message about unresolved externals (someFunction and other templates). Obviously I should not use __declspec on templates but how then to make a class with __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).

You could also follow this link for a more detailed description.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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