CMake错误:即使在手动定义变量后,也将其设置为NOTFOUND

4xrmg8kj  于 2023-02-12  发布在  其他
关注(0)|答案(1)|浏览(257)

我是cmake的新手,正在尝试构建一个依赖于GLEW的现有存储库。我已经使用Homebrew安装了GLEW,现在正在尝试运行cmake .。配置步骤完成,但生成步骤引发以下错误:

CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
GLEW_LIBRARY

我已检查/尝试以下操作:

  • CMakeLists.txt包含一行find_package(GLEW REQUIRED),它没有失败,我甚至添加了一行FIND_LIBRARY(GLEW_LIBRARY NAMES libGLEW.dylib PATHS /opt/local/lib /usr/local/lib /usr/lib REQUIRED)来显式地告诉cmake在哪里查找库,它找到了正确的路径。
  • 有一个文件FindGlew.cmake被放在/usr/local/Cellar/cmake/3.25.2/share/cmake/Modules中(我假设是在GLEW的自制安装过程中)。它包含一行unset(GLEW_LIBRARY)。我有点犹豫是否要修改这个文件(应该没有必要,对吗?),但我尝试注解掉这一行并再次运行cmake,但没有任何效果。
  • CMakeCache.txt包含变量GLEW_LIBRARY_DEBUGGLEW_LIBRARY_RELEASE,它们被设置为GLEW_LIBRARY_DEBUG-NOTFOUND等。我手动将它们编辑为libGLEW.dylib文件的路径,并添加了一个名为GLEW_LIBRARY的附加路径,但没有效果。
  • CMakeCache.txt还包含一个已定义的变量GLEW_DIR,还有一个GLEW_LIBRARY_DIR也是NOTFOUND
  • 我使用cmake . -DGLEW_LIBRARY=/usr/local/lib/libGLEW.dylib将该变量作为显式命令传递。我尝试了先删除缓存和不先删除缓存两种情况。
  • 使用this answer中的方法静态定义库。
  • brew reinstall glew.

我也安装了OpenGL,但是是从源代码构建的(不是通过Homebrew)。这会不会和它们没有正确链接有关?OpenGL是由CMakeLists.txt正确找到的,所以cmake必须以某种方式访问它的路径。
编辑:这是CMakeLists.txt文件(的MWE):

cmake_minimum_required (VERSION 3.15)

project ("ProjectName" LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(OpenGL REQUIRED)
include_directories( ${OPENGL_INCLUDE_DIRS} )
# set(CMAKE_FIND_DEBUG_MODE TRUE)
find_package(GLEW REQUIRED)
# set(CMAKE_FIND_DEBUG_MODE FALSE)
include_directories(${GLEW_INCLUDE_DIRS})

file(GLOB sources CONFIGURE_DEPENDS src/*.cpp src/*.hpp *src/.h)
add_executable(${PROJECT_NAME} ${sources})

target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES} ${GLEW_LIBRARIES})

file(COPY ${RESOURCE_FILES} DESTINATION ${CMAKE_BINARY_DIR}/Resources)

编辑:为了完整起见,下面是完整的输出/错误消息:

-- The CXX compiler identification is AppleClang 14.0.0.14000029
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenGL: /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk/System/Library/Frameworks/OpenGL.framework   
-- Found GLEW: /usr/local/lib/cmake/glew/glew-config.cmake  
-- Configuring done
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
GLEW_LIBRARY
    linked by target "ProjectName" in directory /Users/user1/dev/project

-- Generating done
CMake Generate step failed.  Build files cannot be regenerated correctly.
lxkprmvk

lxkprmvk1#

编辑2:根据FindGLEW,您应该使用导入的目标之一-GLEW::GLEW,即

target_link_libraries(your_target GLEW::GLEW ... )

我也会恳请您张贴新的错误,您正在获得相关的MRE您张贴。我会相应地更新我的答案。
编辑:因为你提到有些人能编译,有些人不能。看看这行:

find_package(GLEW CONFIG QUIET)

if(GLEW_FOUND)
#...

这一行试图找到实际的GLEWConfig.cmake文件来配置它。这意味着如果你已经安装了包或者他们以一种你拥有这个GLEWConfig.cmake文件的方式拥有,那么你会得到不同的结果。
我的建议是:弄清楚他们是如何安装GLEW的(或者他们是否编译了它),然后重新创建完全相同的环境。
这里是FindGLEW.cmake的链接。在第41 - 58行你可以看到你可以在你的项目中使用的变量。有NOGLEW_LIBRARY
如果你看一下文件,你就会明白为什么:

if(GLEW_FOUND)
  find_package_handle_standard_args(GLEW DEFAULT_MSG GLEW_CONFIG)
  get_target_property(GLEW_INCLUDE_DIRS GLEW::GLEW INTERFACE_INCLUDE_DIRECTORIES)
  set(GLEW_INCLUDE_DIR ${GLEW_INCLUDE_DIRS})
  get_target_property(_GLEW_DEFS GLEW::GLEW INTERFACE_COMPILE_DEFINITIONS)
  if("${_GLEW_DEFS}" MATCHES "GLEW_STATIC")
    get_target_property(GLEW_LIBRARY_DEBUG GLEW::GLEW IMPORTED_LOCATION_DEBUG)
    get_target_property(GLEW_LIBRARY_RELEASE GLEW::GLEW IMPORTED_LOCATION_RELEASE)
  else()
    get_target_property(GLEW_LIBRARY_DEBUG GLEW::GLEW IMPORTED_IMPLIB_DEBUG)
    get_target_property(GLEW_LIBRARY_RELEASE GLEW::GLEW IMPORTED_IMPLIB_RELEASE)
  endif()
  get_target_property(_GLEW_LINK_INTERFACE GLEW::GLEW IMPORTED_LINK_INTERFACE_LIBRARIES_RELEASE) # same for debug and release
  list(APPEND GLEW_LIBRARIES ${_GLEW_LINK_INTERFACE})
  list(APPEND GLEW_LIBRARY ${_GLEW_LINK_INTERFACE})
  select_library_configurations(GLEW)
  if("${_GLEW_DEFS}" MATCHES "GLEW_STATIC")
    set(GLEW_STATIC_LIBRARIES ${GLEW_LIBRARIES})
  else()
    set(GLEW_SHARED_LIBRARIES ${GLEW_LIBRARIES}) 
  endif()
  unset(_GLEW_DEFS)
  unset(_GLEW_LINK_INTERFACE)
  unset(GLEW_LIBRARY) #<-- here the temporary variable is unset
  unset(GLEW_LIBRARY_DEBUG)
  unset(GLEW_LIBRARY_RELEASE)
  return()
endif()

GLEW_LIBRARY是用于设置其他变量的临时变量。
您要使用的替代方法是:GLEW_LIBRARIES

相关问题