How to link libraries in cmake
You are doing that wrong way. You should use find_package
instead hard-coding paths.
This should go more or less like this:
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
add_library(mainScr
scr/Carte.cpp
scr/Enemy.cpp
scr/MoveableSquare.cpp
scr/Palette.cpp
scr/Player.cpp
scr/Square.cpp
scr/Wall.cpp
scr/glad.c)
target_link_libraries(mainScr PUBLIC ${GLFW_LIBRARIES})
target_include_directories(mainScr PUBLIC ${GLFW_INCLUDE_DIRS})
add_executable(PackMan scr/main.cpp)
This should work if GLFW is properly installed. On Windows you can use vcpkg to manage c++ libraries.
This is done based on GLFW documentation - didn't test this.
how to properly link mingw libraries using cmake and vcpkg?
I am rather late, but here it in case it helps:
Specifying library locations by hand shouldn't be required, the problem is somewhere else.
This error is at build time so it seems that it is passed cmake configuration without errors. Can you post the cmake conf log? perhaps if you tried specifying both --triplet=
and --host-triplet=
?
How to properly link to libraries in CMake (using Boehm GC)?
Because you use a separate CMake invocation to create the executable, the properties of the Runtime
CMake target from your first project are not known. Specifically, CMake will not know any of the Runtime
library's dependencies (i.e. GC
), so you have to list them explicitly when linking Runtime
to your executable:
cmake_minimum_required(VERSION 3.15)
project(test)
set(CMAKE_CXX_STANDARD 17)
add_executable(test main.cpp)
find_library(TESTLIB Runtime lib)
message(${TESTLIB})
# Find GC library.
find_library(GC gc)
# Link GC here, along with the Runtime library.
target_link_libraries(test PRIVATE ${GC} ${TESTLIB})
Related Topics
Is There a Range Class in C++11 for Use with Range Based for Loops
Copy Map Values to Vector in Stl
How to Handle Failure in Constructor in C++
Why Should I Use the "Using" Keyword to Access My Base Class Method
"Relocation R_X86_64_32S Against " Linking Error
Why Does Int*[] Decay into Int** But Not Int[][]
C++ Array Assignment of Multiple Values
Linking a Shared Library with Another Shared Lib in Linux
Seeking and Reading Large Files in a Linux C++ Application
Shared-Memory Ipc Synchronization (Lock-Free)
Does Std::List::Remove Method Call Destructor of Each Removed Element
How to Best Handle Dynamic Multi-Dimensional Arrays in C/C++
C++ Convert Vector<Int> to Vector<Double>
Error with Multiple Definitions of Function
C++ Inlining Class Methods Causes Undefined Reference
When Is a C++ Destructor Called