How to Properly Link Libraries with Cmake

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



Leave a reply



Submit