繁体   English   中英

CMake find_package 与 GLEW

[英]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.

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