Home > Back-end >  How to link libraries in cmake
How to link libraries in cmake

Time:05-20

I am trying to pass my c project that I was developing in Linux to windows.

I am using cLion so a cMake.

this is my Cmake

   cmake_minimum_required(VERSION 3.10) # common to every CLion project
    project(PackMan) # project name


    set(GLM_DIR C:/libs/GLM/glm)
    set(GLAD_DIR C:/libs/GLAD/include)

    include_directories(${GLM_DIR})
    include_directories(${GLAD_DIR})

    find_package(PkgConfig REQUIRED)
    pkg_search_module(GLFW REQUIRED glfw)

    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
    )


    add_executable(PackMan scr/main.cpp)
    target_link_libraries(PackMan libglfw3.a)
    target_link_libraries(PackMan mainScr)

Every include folder works fine.

I copie pasted dll file ro systeme32 folder inside windows folder. So like I said in my project I have all external includes i can see where the definition is made and everything but it seems like I cant link them with dll.

I get the error of

-- Checking for one of the modules 'glfw'
CMake Error at C:/Program Files/JetBrains/CLion 2022.1.1/bin/cmake/win/share/cmake-3.22/Modules/FindPkgConfig.cmake:890 (message):
  None of the required 'glfw' found
Call Stack (most recent call first):
  CMakeLists.txt:12 (pkg_search_module)


-- Configuring incomplete, errors occurred!
See also "C:/Users/tanku/Documents/Projects/PackMan/cmake-build-debug/CMakeFiles/CMakeOutput.log".

when I try to build.

CodePudding user response:

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.

  • Related