簡體   English   中英

通過CMake將外部庫包含到CLion項目中

[英]Include external libraries into CLion project through CMake

我很難在CLion Project中使用GLFW Windows預編譯的二進制文件 這些庫位於外部目錄中。 不希望它們出現在我的項目庫中,但是(當然)應該在發布應用程序時將它們運送出去。 我是C ++的新手,但我想實現這一點可能和在Java中一樣容易(Intellij Idea->依賴項-> ...)。

GLFW Windows預編譯的二進制文件

我使用MinGW 5.0和CMake 3.10.2; 我的CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)
project(Hatsudouki_core)

set(CMAKE_CXX_STANDARD 17)

link_directories(F:\\C++\\ExternalLibraries\\GLFW\\lib-mingw-w64)
include_directories(F:\\C++\\ExternalLibraries\\GLFW\\include)

add_executable(Hatsudouki_core main.cpp)

target_link_libraries(Hatsudouki_core glfw3)

Main.cpp的

#include <iostream>
#include <GLFW/glfw3.h>

int main() {
    if (!glfwInit())
         std::cout << "error!" << std::endl;
    else
         std::cout << "success!" << std::endl;
    return 0;
}

建立輸出

"F:\C++\CLion 2018.1\bin\cmake\bin\cmake.exe" --build C:\Users\simon\CLionProjects\Hatsudouki-core\cmake-build-debug --target Hatsudouki_core -- -j 4
[ 50%] Linking CXX executable Hatsudouki_core.exe
CMakeFiles\Hatsudouki_core.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/simon/CLionProjects/Hatsudouki-core/main.cpp:5: undefined reference to `glfwInit'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [Hatsudouki_core.exe] Error 1
CMakeFiles\Hatsudouki_core.dir\build.make:96: recipe for target 'Hatsudouki_core.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Hatsudouki_core.dir/all' failed
mingw32-make.exe[2]: *** [CMakeFiles/Hatsudouki_core.dir/all] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/Hatsudouki_core.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/Hatsudouki_core.dir/rule] Error 2
Makefile:117: recipe for target 'Hatsudouki_core' failed
mingw32-make.exe: *** [Hatsudouki_core] Error 2

我嘗試了以下提到的解決方案:
-GLFW docGLFW doc2 (查找包不起作用,沒有CMake文件)
- Github上的問題報告涉及到Github的問題的報告2然后導致該解決方案把FindGLFW.cmake到某個目錄? 試圖把它放在這里GLFW\\FindGLFW.cmake但不起作用
-鏈接不能像這里提到的那樣正常工作: Stackoverflow

圖像GLFW目錄: GLFW Windows預編譯的二進制文件

我想我只是不了解CMake,外部庫和C ++如何共同完成這項相當簡單的任務。 我相信與Java比較會有所幫助(用於gradle)

編輯1如建議,我添加了以下內容:我將Findglfw3.cmake放入PROJECT/cmake/Modules/

# Copyright (c) 2015 Andrew Kelley
# This file is MIT licensed.
# See http://opensource.org/licenses/MIT

# GLFW_FOUND
# GLFW_INCLUDE_DIR
# GLFW_LIBRARY

find_path(GLFW_INCLUDE_DIR NAMES F:\\C++\\ExternalLibraries\\GLFW\\include\\GLFW\\glfw3.h)

find_library(GLFW_LIBRARY NAMES glfw glfw3)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(GLFW DEFAULT_MSG GLFW_LIBRARY GLFW_INCLUDE_DIR)

mark_as_advanced(GLFW_INCLUDE_DIR GLFW_LIBRARY)

並將以下幾行添加到我的CMakeLists.txt中:

find_package(glfw3 REQUIRED)
include_directories(${glfw3_INCLUDE_DIRS})
set(LIBS ${LIBS} ${glfw3_LIBRARIES})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")

target_link_libraries(hatsudouki_core ${LIBS})

我也在Findglfw3.cmake中嘗試過:

find_path(GLFW_INCLUDE_DIR NAMES GLFW/glfw3.h)

與原始文件中的相同。 兩者都不起作用; 錯誤:

"F:\C++\CLion 2018.1\bin\cmake\bin\cmake.exe" --build C:\Users\simon\CLionProjects\Hatsudouki-core\cmake-build-debug --target Hatsudouki_core -- -j 4
CMake Error at CMakeLists.txt:6 (find_package):
-- Configuring incomplete, errors occurred!
By not providing "Findglfw3.cmake" in CMAKE_MODULE_PATH this project has
See also "C:/Users/simon/CLionProjects/Hatsudouki-core/cmake-build-debug/CMakeFiles/CMakeOutput.log".
asked CMake to find a package configuration file provided by "glfw3", but
CMake did not find one.

Makefile:175: recipe for target 'cmake_check_build_system' failed
Could not find a package configuration file provided by "glfw3" with any of
the following names:

glfw3Config.cmake
glfw3-config.cmake

Add the installation prefix of "glfw3" to CMAKE_PREFIX_PATH or set
"glfw3_DIR" to a directory containing one of the above files.  If "glfw3"
provides a separate development package or SDK, be sure it has been
installed.
mingw32-make.exe: *** [cmake_check_build_system] Error 1

這里解釋

  1. PROJECT/cmake/Modules/制作Findglfw3.cmake文件,如下所示

     # GLFW_FOUND # GLFW_INCLUDE_DIR # GLFW_LIBRARY set(FIND_GLFW_PATHS "F:\\\\C++\\\\ExternalLibraries\\\\GLFW") find_path(GLFW_INCLUDE_DIR NAMES GLFW/glfw3 GLFW/glfw3.h PATH_SUFFIXES include PATHS ${FIND_GLFW_PATHS}) find_library(GLFW_LIBRARY NAMES glfw3 glfw3.a libglfw3 libglfw3.a PATH_SUFFIXES lib-mingw PATHS ${FIND_GLFW_PATHS}) include(FindPackageHandleStandardArgs) find_package_handle_standard_args(GLFW DEFAULT_MSG GLFW_LIBRARY GLFW_INCLUDE_DIR) mark_as_advanced(GLFW_INCLUDE_DIR GLFW_LIBRARY) 
  2. CMakeLists.txt定義模塊路徑

     #Define module path list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/Modules") 
  3. 還鏈接包含目錄和庫與CMakeLists.txt項目

     #Define static GLFW libraries and header files find_package(glfw3 REQUIRED) include_directories(${GLFW_INCLUDE_DIR}) ... target_link_libraries(Hatsudouki_core ${GLFW_LIBRARY}) 

首先,您需要安裝glfw軟件包。 如果使用的是MSYS2則可以在64位Windows上使用pacman -S mingw-w64-x86_64-glfw來安裝, pacman -S mingw-w64-i686-glfw在32位Windows上pacman -S mingw-w64-i686-glfw安裝。 並且您需要使用find_package來讓cmake自動定位glfw庫,而不是手動定位它們。 如果您使用MSYS2則可能會有點困難 查看此代碼。

cmake_minimum_required(VERSION 3.10)
project(Hatsudouki_core)

set(CMAKE_CXX_STANDARD 17)

add_executable(Hatsudouki_core main.cpp)

find_package(glfw3 REQUIRED) # Find the GLFW library
target_link_libraries(Hatsudouki_core PRIVATE glfw) # Finally, link to them.

如果需要預編譯的庫,建議您使用MSYS2。 它有一個非常好的軟件包管理器,稱為pacman

暫無
暫無

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

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