Stibius
Stibius

Reputation: 93

CMake find_package with GLEW

I am trying to set up a CMakeLists.txt file for my project which uses GLEW. I have been doing it like this:

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})

This approach works but I want to use the find_package approach because I think it is better.

I have found some examples on this site like this one: Adding GLEW to project (CMake) which suggests doing something like this:

find_package(GLEW REQUIRED)
target_link_libraries(${APP_NAME} GLEW::GLEW)

But when I try to to do this, it produces errors during the configuration phase:

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)

So it looks like it is looking for glew-targets.cmake in C:/libs/glew-2.1.0/build/cmake which is the GLEW_DIR I set in CMake GUI because it has the glew-config.cmake file.

But the glew-targets.cmake file is actually in C:\libs\glew-2.1.0\build\cmake\build\CMakeFiles\Export\lib\cmake\glew where it got created when I ran CMake on GLEW.

Why is it looking in the wrong place?

I downloaded GLEW as a .ZIP source from http://glew.sourceforge.net/. I then ran CMake on it while specifying C:/libs/glew-2.1.0/build/cmake as the source directory and C:/libs/glew-2.1.0/build/cmake/build as the build directory.

So how exactly I am supposed to do this?

Upvotes: 6

Views: 2720

Answers (1)

Guillaume Racicot
Guillaume Racicot

Reputation: 41780

This approach works but I want to use the find_package approach because I think it is better.

It is better, since you cannot know all the requirements glew needs in order to be used. The find_package command has all the necessary to detect requirements from supported libraries (or libraries that support CMake).

So it looks like it is looking for glew-targets.cmake in C:/libs/glew-2.1.0/build/cmake which is the GLEW_DIR I set in CMake GUI because it has the glew-config.cmake file.

The thing is GLEW don't support importing from it's build tree.

To make it work, simply set a install prefix when building GLEW with CMake:

-DCMAKE_INSTALL_PREFIX=C:/libs/glew-2.1.0/build/cmake/install

Then, install the library. It should install in the specified directory.

Then, you should set the glew_DIR variable to that directory:

set(glew_DIR "C:/libs/glew-2.1.0/build/cmake/install/lib/cmake/glew")

You can alternatively set the prefix path to the installation directory:

list(APPEND CMAKE_PREFIX_PATH "C:/libs/glew-2.1.0/build/cmake/install")

It finds the wrong file because inside their source directory, glew has a file named glew-config.cmake (IMO it should be in a subfolder to avoid situation like this one).

The build directory should also contain the config file.

Upvotes: 1

Related Questions