开发者

C++ template dll => unresolved externals

开发者 https://www.devze.com 2023-02-18 08:10 出处:网络
I have a template function in a shared library. I know the function will be called with either int or double arguments. I therefore instan开发者_如何学Pythontiate the two versions of the template in t

I have a template function in a shared library. I know the function will be called with either int or double arguments. I therefore instan开发者_如何学Pythontiate the two versions of the template in the source file.

template void library::doSomething<int>(int const number);
template void library::doSomething<double>(double const number);

This solution works with g++ on Linux (getting a *.so), but when I try to compile the same code into a *.dll on Windows using VS2010 I get an error like this:

error LNK2001: unresolved external symbol doSomething

Exports are provided in a *.def file like:

EXPORTS

doSomething

Am I missing something or is this solution "incompatible" with Windows?

Thanks.

Petr


You need to put the mangled names of the instantiated functions into the DEF file, not the C++ names.

You can find out more in the MSDN article Exporting from a DLL Using DEF Files:

If you are exporting functions in a C++ file, you have to either place the decorated names in the .def file or define your exported functions with standard C linkage by using extern "C".

If you need to place the decorated names in the .def file, you can obtain them by using the DUMPBIN tool or by using the linker /MAP option. Note that the decorated names produced by the compiler are compiler specific.

If you place the decorated names produced by the Visual C++ compiler into a .def file, applications that link to your DLL must also be built using the same version of Visual C++ so that the decorated names in the calling application match the exported names in the DLL's .def file.

Usually, using __declspec(dllexport) is much more straightforward and if you can use it you should consider doing so.

0

精彩评论

暂无评论...
验证码 换一张
取 消