How to link libraries in cmake

Onur Kulan

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.

Marek R

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to link static libraries in cmake

How to cross link libraries in CMake

How to link all LLVM libraries in cmake?

CMAKE: how to include and link alternative libraries?

CMake: how to link libraries from a parallel dir

How to auto-link boost libraries with CMake

[cmake]how to include and link system libraries on windows using cmake

CMake: Target Link Libraries

cmake link two libraries +

How to use cmake's target_link_libraries to link libraries matching a glob?

How to find and link CUDA libraries using CMake 3.15?

How to force cmake link against custom gcc libraries

How to link OpenGL related libraries on mac using cmake?

How to properly link to libraries in CMake (using Boehm GC)?

Cmake: How to use g++ to link itk or other libraries

How to link bullet libraries(from FindBullet.cmake)?

CMake link libraries no header found

Which libraries are available to link in cmake?

how do i specify libraries's path to cmake which are used by target_link_libraries

How can I make `target_link_libraries` dependencies transitive from OBJECT libraries in cmake?

How to build libraries with CMake

cmake: how to set rpath in a shared library with only target_link_directories (without target_link_libraries)?

Recursive list of LINK_LIBRARIES in CMake

CMake OpenCV Cannot Specify Link Libraries

CMake and QT won't link to QT libraries

CMake target_link_libraries Interface Dependencies

CMAKE finds package but does not link libraries

Cmake OpenCV Can not specify link libraries

Set LINK_FLAGS for INTERFACE libraries in cmake