macoscmakeglew

CMake Error: variable set to NOTFOUND even after defining it manually


I am new to cmake and attempting to build an existing repository that relies on GLEW. I have installed GLEW using Homebrew and am now trying to run cmake . The configuration step completes, but the generation step raises the following error:

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

I have checked/tried the following:

I do have OpenGL installed as wel, but built from source (not via Homebrew). Could It have something to do with them not being linked correctly? OpenGL is found properly by CMakeLists.txt, so cmake must have access to its path somehow.

Edit: this is (a MWE of) the CMakeLists.txt file:

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)

Edit: for completeness, here is the full output / error message:

-- 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.

Solution

  • EDIT2: According to FindGLEW you should use one of the imported targets - GLEW::GLEW i.e.

    target_link_libraries(your_target GLEW::GLEW ... )
    

    I would also kindly ask you to post the new errors that you are getting related to the MRE you posted. I will update my answer accordingly.


    EDIT: Because you mentioned that some people can compile it and some people can't. Look at this line right here:

    find_package(GLEW CONFIG QUIET)
    
    if(GLEW_FOUND)
    #...
    

    This line tries to find the actual GLEWConfig.cmake file to configure this. Meaning that if you've installed the package OR they have in a way that you have this GLEWConfig.cmake file then you will get different results.

    My recommendation is: Figure out exactly how they installed their GLEW (or if they compiled it) and recreate the exact same environment.


    Here is the link to FindGLEW.cmake. On Lines 41-58 you can see the variables that you can use in your project. There is NO GLEW_LIBRARY.

    If you look down in the file you'll see why:

    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 is a temporary variable used to set other variables.

    What instead you want to use is: GLEW_LIBRARIES