如何在cmake中链接库

荣誉

我正在尝试将我在 Linux 中开发的 c++ 项目传递给 Windows。

我正在使用 cLion 所以一个 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)

每个包含文件夹都可以正常工作。

我在 windows 文件夹中复制粘贴的 dll 文件 ro systeme32 文件夹。所以就像我在我的项目中所说的那样,我拥有所有外部包含,我可以看到定义的位置以及所有内容,但似乎我无法将它们与 dll 链接。

我得到的错误

-- 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".

当我尝试构建时。

马雷克

你这样做是错误的。您应该find_package改用硬编码路径。

这应该或多或少像这样:

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)

如果正确安装了 GLFW,这应该可以工作。在 Windows 上,您可以使用 vcpkg 来管理 c++ 库。

这是根据GLFW 文档完成的- 没有对此进行测试。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章