简体   繁体   English

如何在cmake中链接库

[英]How to link libraries in cmake

I am trying to pass my c++ project that I was developing in Linux to windows.我正在尝试将我在 Linux 中开发的 c++ 项目传递给 Windows。

I am using cLion so a cMake.我正在使用 cLion 所以一个 cMake。

this is my Cmake这是我的 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.我在 windows 文件夹中复制粘贴的 dll 文件 ro systeme32 文件夹。 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.所以就像我在我的项目中说的那样,我有所有外部包含,我可以看到定义的位置以及所有内容,但似乎我无法将它们与 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.当我尝试构建时。

You are doing that wrong way.你这样做是错误的。 You should use find_package instead hard-coding paths.您应该使用find_package代替硬编码路径。

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.如果正确安装了 GLFW,这应该可以工作。 On Windows you can use vcpkg to manage c++ libraries.在 Windows 上,您可以使用 vcpkg 来管理 c++ 库。

This is done based on GLFW documentation - didn't test this.这是根据GLFW 文档完成的 - 没有对此进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM