[英]CMake find_package with GLEW
我正在嘗試為使用 GLEW 的項目設置 CMakeLists.txt 文件。 我一直這樣做:
find_path(GLEW_INCLUDE_DIR GL/glew.h)
find_library(GLEW_LIBRARY_RELEASE glew32)
find_library(GLEW_LIBRARY_DEBUG glew32d)
target_include_directories(${APP_NAME} PUBLIC ${GLEW_INCLUDE_DIR})
target_link_libraries(${APP_NAME} optimized ${GLEW_LIBRARY_RELEASE}
debug ${GLEW_LIBRARY_DEBUG})
這種方法有效,但我想使用 find_package 方法,因為我認為它更好。
我在這個網站上找到了一些這樣的例子: 將 GLEW 添加到項目 (CMake)這建議做這樣的事情:
find_package(GLEW REQUIRED)
target_link_libraries(${APP_NAME} GLEW::GLEW)
但是當我嘗試這樣做時,它會在配置階段產生錯誤:
CMake Error at C:/libs/glew-2.1.0/build/cmake/glew-config.cmake:16 (include):
include could not find load file:
C:/libs/glew-2.1.0/build/cmake/glew-targets.cmake
Call Stack (most recent call first):
C:/Program Files/CMake/share/cmake-3.15/Modules/FindGLEW.cmake:62 (find_package)
CMakeLists.txt:14 (find_package)
CMake Error at C:/libs/glew-2.1.0/build/cmake/CopyImportedTargetProperties.cmake:64 (get_property):
get_property could not find TARGET GLEW::glew. Perhaps it has not yet been
created.
Call Stack (most recent call first):
C:/libs/glew-2.1.0/build/cmake/glew-config.cmake:56 (copy_imported_target_properties)
C:/Program Files/CMake/share/cmake-3.15/Modules/FindGLEW.cmake:62 (find_package)
CMakeLists.txt:14 (find_package)
CMake Error at C:/libs/glew-2.1.0/build/cmake/CopyImportedTargetProperties.cmake:64 (get_property):
get_property could not find TARGET GLEW::glew. Perhaps it has not yet been
created.
Call Stack (most recent call first):
C:/libs/glew-2.1.0/build/cmake/glew-config.cmake:56 (copy_imported_target_properties)
C:/Program Files/CMake/share/cmake-3.15/Modules/FindGLEW.cmake:62 (find_package)
CMakeLists.txt:14 (find_package)
CMake Error at C:/libs/glew-2.1.0/build/cmake/CopyImportedTargetProperties.cmake:64 (get_property):
get_property could not find TARGET GLEW::glew. Perhaps it has not yet been
created.
Call Stack (most recent call first):
C:/libs/glew-2.1.0/build/cmake/glew-config.cmake:56 (copy_imported_target_properties)
C:/Program Files/CMake/share/cmake-3.15/Modules/FindGLEW.cmake:62 (find_package)
CMakeLists.txt:14 (find_package)
CMake Error at C:/libs/glew-2.1.0/build/cmake/CopyImportedTargetProperties.cmake:64 (get_property):
get_property could not find TARGET GLEW::glew. Perhaps it has not yet been
created.
Call Stack (most recent call first):
C:/libs/glew-2.1.0/build/cmake/glew-config.cmake:56 (copy_imported_target_properties)
C:/Program Files/CMake/share/cmake-3.15/Modules/FindGLEW.cmake:62 (find_package)
CMakeLists.txt:14 (find_package)
CMake Error at C:/libs/glew-2.1.0/build/cmake/CopyImportedTargetProperties.cmake:64 (get_property):
get_property could not find TARGET GLEW::glew. Perhaps it has not yet been
created.
Call Stack (most recent call first):
C:/libs/glew-2.1.0/build/cmake/glew-config.cmake:56 (copy_imported_target_properties)
C:/Program Files/CMake/share/cmake-3.15/Modules/FindGLEW.cmake:62 (find_package)
CMakeLists.txt:14 (find_package)
CMake Error at C:/libs/glew-2.1.0/build/cmake/CopyImportedTargetProperties.cmake:64 (get_property):
get_property could not find TARGET GLEW::glew. Perhaps it has not yet been
created.
Call Stack (most recent call first):
C:/libs/glew-2.1.0/build/cmake/glew-config.cmake:56 (copy_imported_target_properties)
C:/Program Files/CMake/share/cmake-3.15/Modules/FindGLEW.cmake:62 (find_package)
CMakeLists.txt:14 (find_package)
CMake Error at C:/libs/glew-2.1.0/build/cmake/CopyImportedTargetProperties.cmake:64 (get_property):
get_property could not find TARGET GLEW::glew. Perhaps it has not yet been
created.
Call Stack (most recent call first):
C:/libs/glew-2.1.0/build/cmake/glew-config.cmake:56 (copy_imported_target_properties)
C:/Program Files/CMake/share/cmake-3.15/Modules/FindGLEW.cmake:62 (find_package)
CMakeLists.txt:14 (find_package)
CMake Error at C:/libs/glew-2.1.0/build/cmake/CopyImportedTargetProperties.cmake:64 (get_property):
get_property could not find TARGET GLEW::glew. Perhaps it has not yet been
created.
Call Stack (most recent call first):
C:/libs/glew-2.1.0/build/cmake/glew-config.cmake:56 (copy_imported_target_properties)
C:/Program Files/CMake/share/cmake-3.15/Modules/FindGLEW.cmake:62 (find_package)
CMakeLists.txt:14 (find_package)
CMake Error at C:/libs/glew-2.1.0/build/cmake/CopyImportedTargetProperties.cmake:64 (get_property):
get_property could not find TARGET GLEW::glew. Perhaps it has not yet been
created.
Call Stack (most recent call first):
C:/libs/glew-2.1.0/build/cmake/glew-config.cmake:56 (copy_imported_target_properties)
C:/Program Files/CMake/share/cmake-3.15/Modules/FindGLEW.cmake:62 (find_package)
CMakeLists.txt:14 (find_package)
CMake Error at C:/libs/glew-2.1.0/build/cmake/CopyImportedTargetProperties.cmake:64 (get_property):
get_property could not find TARGET GLEW::glew. Perhaps it has not yet been
created.
Call Stack (most recent call first):
C:/libs/glew-2.1.0/build/cmake/glew-config.cmake:56 (copy_imported_target_properties)
C:/Program Files/CMake/share/cmake-3.15/Modules/FindGLEW.cmake:62 (find_package)
CMakeLists.txt:14 (find_package)
CMake Error at C:/libs/glew-2.1.0/build/cmake/CopyImportedTargetProperties.cmake:64 (get_property):
get_property could not find TARGET GLEW::glew. Perhaps it has not yet been
created.
Call Stack (most recent call first):
C:/libs/glew-2.1.0/build/cmake/glew-config.cmake:56 (copy_imported_target_properties)
C:/Program Files/CMake/share/cmake-3.15/Modules/FindGLEW.cmake:62 (find_package)
CMakeLists.txt:14 (find_package)
CMake Error at C:/libs/glew-2.1.0/build/cmake/CopyImportedTargetProperties.cmake:64 (get_property):
get_property could not find TARGET GLEW::glew. Perhaps it has not yet been
created.
Call Stack (most recent call first):
C:/libs/glew-2.1.0/build/cmake/glew-config.cmake:56 (copy_imported_target_properties)
C:/Program Files/CMake/share/cmake-3.15/Modules/FindGLEW.cmake:62 (find_package)
CMakeLists.txt:14 (find_package)
CMake Error at C:/libs/glew-2.1.0/build/cmake/CopyImportedTargetProperties.cmake:64 (get_property):
get_property could not find TARGET GLEW::glew. Perhaps it has not yet been
created.
Call Stack (most recent call first):
C:/libs/glew-2.1.0/build/cmake/glew-config.cmake:56 (copy_imported_target_properties)
C:/Program Files/CMake/share/cmake-3.15/Modules/FindGLEW.cmake:62 (find_package)
CMakeLists.txt:14 (find_package)
CMake Error at C:/libs/glew-2.1.0/build/cmake/CopyImportedTargetProperties.cmake:64 (get_property):
get_property could not find TARGET GLEW::glew. Perhaps it has not yet been
created.
Call Stack (most recent call first):
C:/libs/glew-2.1.0/build/cmake/glew-config.cmake:56 (copy_imported_target_properties)
C:/Program Files/CMake/share/cmake-3.15/Modules/FindGLEW.cmake:62 (find_package)
CMakeLists.txt:14 (find_package)
CMake Error at C:/libs/glew-2.1.0/build/cmake/CopyImportedTargetProperties.cmake:64 (get_property):
get_property could not find TARGET GLEW::glew. Perhaps it has not yet been
created.
Call Stack (most recent call first):
C:/libs/glew-2.1.0/build/cmake/glew-config.cmake:56 (copy_imported_target_properties)
C:/Program Files/CMake/share/cmake-3.15/Modules/FindGLEW.cmake:62 (find_package)
CMakeLists.txt:14 (find_package)
CMake Error at C:/libs/glew-2.1.0/build/cmake/CopyImportedTargetProperties.cmake:64 (get_property):
get_property could not find TARGET GLEW::glew. Perhaps it has not yet been
created.
Call Stack (most recent call first):
C:/libs/glew-2.1.0/build/cmake/glew-config.cmake:56 (copy_imported_target_properties)
C:/Program Files/CMake/share/cmake-3.15/Modules/FindGLEW.cmake:62 (find_package)
CMakeLists.txt:14 (find_package)
CMake Error at C:/libs/glew-2.1.0/build/cmake/CopyImportedTargetProperties.cmake:64 (get_property):
get_property could not find TARGET GLEW::glew. Perhaps it has not yet been
created.
Call Stack (most recent call first):
C:/libs/glew-2.1.0/build/cmake/glew-config.cmake:56 (copy_imported_target_properties)
C:/Program Files/CMake/share/cmake-3.15/Modules/FindGLEW.cmake:62 (find_package)
CMakeLists.txt:14 (find_package)
CMake Error at C:/libs/glew-2.1.0/build/cmake/CopyImportedTargetProperties.cmake:64 (get_property):
get_property could not find TARGET GLEW::glew. Perhaps it has not yet been
created.
Call Stack (most recent call first):
C:/libs/glew-2.1.0/build/cmake/glew-config.cmake:56 (copy_imported_target_properties)
C:/Program Files/CMake/share/cmake-3.15/Modules/FindGLEW.cmake:62 (find_package)
CMakeLists.txt:14 (find_package)
CMake Error at C:/libs/glew-2.1.0/build/cmake/CopyImportedTargetProperties.cmake:73 (get_target_property):
get_target_property() called with non-existent target "GLEW::glew".
Call Stack (most recent call first):
C:/libs/glew-2.1.0/build/cmake/glew-config.cmake:56 (copy_imported_target_properties)
C:/Program Files/CMake/share/cmake-3.15/Modules/FindGLEW.cmake:62 (find_package)
CMakeLists.txt:14 (find_package)
所以它看起來像是在 C:/libs/glew-2.1.0/build/cmake 中尋找 glew-targets.cmake 這是我在 CMake GUI 中設置的 GLEW_DIR 因為它有 glew-config.cmake 文件。
但是 glew-targets.cmake 文件實際上在 C:\\libs\\glew-2.1.0\\build\\cmake\\build\\CMakeFiles\\Export\\lib\\cmake\\glew 中,當我在 GLEW 上運行 CMake 時,它是在那里創建的。
為什么它找錯了地方?
我從http://glew.sourceforge.net/下載了 GLEW 作為 .ZIP 源。 然后我在它上面運行 CMake,同時指定 C:/libs/glew-2.1.0/build/cmake 作為源目錄和 C:/libs/glew-2.1.0/build/cmake/build 作為構建目錄。
那么我到底應該怎么做呢?
這種方法有效,但我想使用 find_package 方法,因為我認為它更好。
更好,因為您無法了解所有的需求才能使用。 find_package
命令具有從受支持的庫(或支持 CMake 的庫)檢測需求的所有必要條件。
所以它看起來像是在 C:/libs/glew-2.1.0/build/cmake 中尋找 glew-targets.cmake 這是我在 CMake GUI 中設置的 GLEW_DIR 因為它有 glew-config.cmake 文件。
問題是 GLEW 不支持從它的構建樹導入。
要使其工作,只需在使用 CMake 構建 GLEW 時設置安裝前綴:
-DCMAKE_INSTALL_PREFIX=C:/libs/glew-2.1.0/build/cmake/install
然后,安裝庫。 它應該安裝在指定的目錄中。
然后,您應該將glew_DIR
變量設置為該目錄:
set(glew_DIR "C:/libs/glew-2.1.0/build/cmake/install/lib/cmake/glew")
您也可以將前綴路徑設置為安裝目錄:
list(APPEND CMAKE_PREFIX_PATH "C:/libs/glew-2.1.0/build/cmake/install")
它找到了錯誤的文件,因為在它們的源目錄中,glew 有一個名為glew-config.cmake
的文件( IMO 應該在一個子文件夾中以避免出現這種情況)。
構建目錄還應包含配置文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.