genaray
genaray

Reputation: 1190

CMake wont link my library, make file does

Background

I recently downloaded a lib called "RayLib" for c and c++. I added the mingw includes and libs into my project and well, now i wanna link them.

enter image description here

The problem

For some reason, cmake does not link the static lib. When i try to run the project, it tells me that there still undefined references to "InitWindow" and "SetTargetFPS" :

====================[ Build | Engine | Debug ]==================================
C:\Users\Lars\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\203.7148.70\bin\cmake\win\bin\cmake.exe --build C:\Users\Lars\CLionProjects\Engine\cmake-build-debug --target Engine -- -j 9
C:\Users\Lars\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\203.7148.70\bin\cmake\win\bin\cmake.exe -SC:\Users\Lars\CLionProjects\Engine -BC:\Users\Lars\CLionProjects\Engine\cmake-build-debug --check-build-system CMakeFiles\Makefile.cmake 0
C:/Users/Lars/Desktop/Development/MinGW/bin/mingw32-make.exe  -f CMakeFiles\Makefile2 Engine
mingw32-make.exe[1]: Entering directory 'C:/Users/Lars/CLionProjects/Engine/cmake-build-debug'
C:\Users\Lars\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\203.7148.70\bin\cmake\win\bin\cmake.exe -SC:\Users\Lars\CLionProjects\Engine -BC:\Users\Lars\CLionProjects\Engine\cmake-build-debug --check-build-system CMakeFiles\Makefile.cmake 0
C:\Users\Lars\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\203.7148.70\bin\cmake\win\bin\cmake.exe -E cmake_progress_start C:\Users\Lars\CLionProjects\Engine\cmake-build-debug\CMakeFiles 2
C:/Users/Lars/Desktop/Development/MinGW/bin/mingw32-make.exe  -f CMakeFiles\Makefile2 CMakeFiles/Engine.dir/all
mingw32-make.exe[2]: Entering directory 'C:/Users/Lars/CLionProjects/Engine/cmake-build-debug'
C:/Users/Lars/Desktop/Development/MinGW/bin/mingw32-make.exe  -f CMakeFiles\Engine.dir\build.make CMakeFiles/Engine.dir/depend
mingw32-make.exe[3]: Entering directory 'C:/Users/Lars/CLionProjects/Engine/cmake-build-debug'
C:\Users\Lars\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\203.7148.70\bin\cmake\win\bin\cmake.exe -E cmake_depends "MinGW Makefiles" C:\Users\Lars\CLionProjects\Engine C:\Users\Lars\CLionProjects\Engine C:\Users\Lars\CLionProjects\Engine\cmake-build-debug C:\Users\Lars\CLionProjects\Engine\cmake-build-debug C:\Users\Lars\CLionProjects\Engine\cmake-build-debug\CMakeFiles\Engine.dir\DependInfo.cmake --color=
mingw32-make.exe[3]: Leaving directory 'C:/Users/Lars/CLionProjects/Engine/cmake-build-debug'
C:/Users/Lars/Desktop/Development/MinGW/bin/mingw32-make.exe  -f CMakeFiles\Engine.dir\build.make CMakeFiles/Engine.dir/build
mingw32-make.exe[3]: Entering directory 'C:/Users/Lars/CLionProjects/Engine/cmake-build-debug'
[ 50%] Linking CXX executable Engine.exe
C:\Users\Lars\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\203.7148.70\bin\cmake\win\bin\cmake.exe -E cmake_link_script CMakeFiles\Engine.dir\link.txt --verbose=1
C:\Users\Lars\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\203.7148.70\bin\cmake\win\bin\cmake.exe -E rm -f CMakeFiles\Engine.dir/objects.a
C:\Users\Lars\Desktop\Development\MinGW\bin\ar.exe cr CMakeFiles\Engine.dir/objects.a @CMakeFiles\Engine.dir\objects1.rsp
C:\Users\Lars\Desktop\Development\MinGW\bin\g++.exe  -Wall -std=c++17 -g   -Wl,--whole-archive CMakeFiles\Engine.dir/objects.a -Wl,--no-whole-archive  -o Engine.exe -Wl,--out-implib,libEngine.dll.a -Wl,--major-image-version,0,--minor-image-version,0 @CMakeFiles\Engine.dir\linklibs.rsp
c:/users/lars/desktop/development/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: CMakeFiles\Engine.dir/objects.a(main.cpp.obj): in function `main':
C:/Users/Lars/CLionProjects/Engine/main.cpp:22: undefined reference to `InitWindow'
c:/users/lars/desktop/development/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/Lars/CLionProjects/Engine/main.cpp:24: undefined reference to `SetTargetFPS'
c:/users/lars/desktop/development/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/Lars/CLionProjects/Engine/main.cpp:28: undefined reference to `WindowShouldClose'
c:/users/lars/desktop/development/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/Lars/CLionProjects/Engine/main.cpp:37: undefined reference to `BeginDrawing'
c:/users/lars/desktop/development/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/Lars/CLionProjects/Engine/main.cpp:39: undefined reference to `ClearBackground'
c:/users/lars/desktop/development/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/Lars/CLionProjects/Engine/main.cpp:41: undefined reference to `DrawText'
c:/users/lars/desktop/development/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/Lars/CLionProjects/Engine/main.cpp:43: undefined reference to `EndDrawing'
c:/users/lars/desktop/development/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/Lars/CLionProjects/Engine/main.cpp:49: undefined reference to `CloseWindow'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [Engine.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/Engine.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/Engine.dir/rule] Error 2
mingw32-make.exe: *** [Engine] Error 2
CMakeFiles\Engine.dir\build.make:108: recipe for target 'Engine.exe' failed
mingw32-make.exe[3]: Leaving directory 'C:/Users/Lars/CLionProjects/Engine/cmake-build-debug'
CMakeFiles\Makefile2:97: recipe for target 'CMakeFiles/Engine.dir/all' failed
mingw32-make.exe[2]: Leaving directory 'C:/Users/Lars/CLionProjects/Engine/cmake-build-debug'
CMakeFiles\Makefile2:104: recipe for target 'CMakeFiles/Engine.dir/rule' failed
mingw32-make.exe[1]: Leaving directory 'C:/Users/Lars/CLionProjects/Engine/cmake-build-debug'
Makefile:140: recipe for target 'Engine' failed

My cmake file looks like this...

cmake_minimum_required(VERSION 3.17)
project(Engine)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_VERBOSE_MAKEFILE ON)

include_directories(include)
include_directories(libs)
link_directories(libs)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++17")
add_executable(Engine main.cpp)
target_link_libraries(Engine raylib)

My own written make file, located in the root which simply adds a -I include, -L libs and -lraylib is able to compile this project.

main: main.o
   g++ -I include ... -o main main.o -L libs -lraylib
main.o: main.cpp
   g++ -I include ... -c main.cpp -L libs -lraylib

Question

Why exactly does target_link_libraries not work ? Why doesnt it link my .a file located in libs/ ? What am i doing wrong here ? Why isnt Cmake able to link my project, but my own makefile is able to do it ?

Any help is welcome !

Upvotes: 1

Views: 416

Answers (1)

sweenish
sweenish

Reputation: 5202

From the documentation about the command link_directories():

This command is rarely necessary and should be avoided where there are other choices. Prefer to pass full absolute paths to libraries where possible, since this ensures the correct library will always be linked. The find_library() command provides the full path, which can generally be used directly in calls to target_link_libraries().

find_library() is hopefully sufficient by itself.

Upvotes: 1

Related Questions