5

I was trying to link a library to my cmake project but I was getting linker errors. I spent 2 hours trying to resolve the problem and I created simple project where I hardcoded all the paths.

CMAKE_MINIMUM_REQUIRED(VERSION 3.0)
PROJECT(Testing CXX)

INCLUDE_DIRECTORIES("B:/Projects/TMH/Libraries/Awesomium/inc")

SET(SOURCE_FILES
        src/Main.cpp
        src/Application.cpp
        src/Window.cpp
)

ADD_EXECUTABLE(Testing ${SOURCE_FILES})

TARGET_LINK_LIBRARIES(Testing B:/Projects/TMH/Libraries/Awesomium/bin/awesomium.dll)

But even that code fails to compile and throws linker errors like this:

CMakeFiles\Testing.dir/objects.a(Application.cpp.obj): In function `ZN9PXLClient11ApplicationD2Ev':
B:/Projects/TMH/Testing/src/Application.cpp:15: undefined reference to `_imp___ZN9Awesomium7WebCore8ShutdownEv'
CMakeFiles\Testing.dir/objects.a(Application.cpp.obj): In function `ZN9PXLClient11Application3RunEv':
B:/Projects/TMH/Testing/src/Application.cpp:20: undefined reference to `_imp___ZN9Awesomium9WebConfigC1Ev'

I either tried to link the .lib file instead of the .dll one. I changed

TARGET_LINK_LIBRARIES(Testing B:/Projects/TMH/Libraries/Awesomium/bin/awesomium.dll)

to

TARGET_LINK_LIBRARIES(Testing B:/Projects/TMH/Libraries/Awesomium/lib/Awesomium.lib)

But I was getting exactly the same errors. I'm 100% sure that both paths are correct.

How to correctly link a dll/lib to cmake project?

DmqCsm
  • 101
  • 2
  • 4
  • 1
    CMake file is fine. It is the **library** who **doesn't provide these symbols** for external usage. Check resources which describe *how to create libraries* and follow them. – Tsyvarev Oct 18 '16 at 17:37
  • I was using the .lib file in a visual studio project and it works correctly. In Visual Studio all I had was to add the library to Additional Dependencies in linker settings, but the same .lib file doesn't work with cmake. – DmqCsm Oct 18 '16 at 18:03
  • Which generator (Visual Studio, MinGW,...) and compiler do you use with CMake? There could be incompatibilities when compile C++ code with different compilers. – Tsyvarev Oct 18 '16 at 18:22
  • For the Visual Studio project I used default VS installation. For the cmake one I'm using MinGW 3.21 with g++ 4.9.3. The lib file I'm trying to use is from Awesomium SDK (http://www.awesomium.com/) – DmqCsm Oct 18 '16 at 18:26
  • So, it looks like incompatibility between gcc compiler used for MinGW and compiler for VS. Try to build the library with gcc, and it should be linked successfully. – Tsyvarev Oct 18 '16 at 19:45
  • I fix this by replace raw path to "brace with double \ . That is "B:\\Projects\\TMH\\Libraries\\Awesomium\\lib\\Awesomium.lib" – vicky Lin Sep 15 '21 at 07:35

0 Answers0