簡體   English   中英

如何在cmake中鏈接庫

[英]How to link libraries in 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 文檔完成的 - 沒有對此進行測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM