简体   繁体   中英

C++ template dll => unresolved externals

I have a template function in a shared library. I know the function will be called with either int or double arguments. I therefore instantiate 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.

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