user1795160
user1795160

Reputation:

Linking errors on library built using cmake

I guess I just made a simple mistake but I'm not getting which.. Anyways I'm working on a library, also I'm using cmake to build the Makefiles for the project: https://github.com/immapoint/NaNO3/blob/master/CMakeLists.txt

Everything works just fine when compiling the library; it builds the following Files:

bin/libNaNO3.dll
lib/libNaNO3.dll.a (I don't like that name as well)

To test the whole thing, I got another project set up, also using cmake. https://github.com/immapoint/NaNO3TestApp/blob/master/CMakeLists.txt

The main file to test the library looks like this: https://github.com/immapoint/NaNO3TestApp/blob/master/src/main.cpp

But when it comes to compiling the main file, I'm getting following errors:

CMakeFiles/NaNO3TestApp.dir/objects.a(main.cpp.obj):main.cpp:(.text+0xbf): undefined reference to `nano::Event<int>::attach(std::function<void(int)> *)`
CMakeFiles/NaNO3TestApp.dir/objects.a(main.cpp.obj):main.cpp:(.text+0xd3): undefined reference to `nano::Event<int>::notify(int)`
[...]ld.exe: CMakeFiles/NaNO3TestApp.dir/objects.a(main.cpp.obj): bad reloc address 0x8 in section `.rdata'

This error occures whether I'm building the project using make/cmake or compiling the source file directly using

g++ -Wall -pedantic -ansi -std=c++0x main.cpp [-L./lib -I./include] -lNaNO3 

So the problem seems not to lie in cmake but in ld. I'm working with CMake version 2.8 and MinGW containing GCC version 4.7.2.

Additional information:

Compiler output with -fPIC: -fPIC

Upvotes: 0

Views: 1086

Answers (1)

Fraser
Fraser

Reputation: 78418

This has nothing to do with CMake or the linker. You need to include the definitions for the nano::Event member functions in the header, not in a separate source file, since templates are instantiated at compile time. By the time the linker gets there, it's too late.

For a fuller explanation, see Why should the implementation and the declaration of a template class be in the same header file? and http://www.parashift.com/c++-faq-lite/templates-defn-vs-decl.html

Upvotes: 1

Related Questions