GLFW with CMake on Windows, GLFW_INCLUDE_DIRS or GLFW_LIBRARIES not set

Hi Guys

Awesome work on GLFW.

I’ve build GLFW 3.2.1 from source and explicitly specified the lib suffix and the installation prefix. After compilation, everything is installed where I expect it to be and the respective glfw3Config.cmake file also installed.

When I then try to use glfw3 in my own application and try to detect it using CMake it seems that the GLFW_INCLUDE_DIRS and GLFW_LIBRARIES variables are not set when finding the glfw3 package, I.e.:

find_package(glfw3 3.2 CONFIG REQUIRED PATH_SUFFIXES "/cmake/glfw3")

CMake definitely found the glfw3Config.cmake (glfw3_DIR is set). Did I miss something in the build configuration to export these variables? Can I force these variables to be set at compile time (of glfw3) on Windows?

According to this example page:http://www.glfw.org/docs/3.0/build.html it also seems to indicate that the variables are perhaps not set on windows on purpose. Is this a higher design consideration I’m missing?

If none of the above, is it possible to do a feature request to have the variables set in the same way for windows as they are set on Unix?

Cheers
-Vinnie

If you’d like to request a feature use the GLFW issue list on Github.

Personally I don’t install libraries, so my process for using glfw with cmake on Windows and other OSs is to put glfw in a known directory (here below the cmakelists.txt dir for my project):

find_package(OpenGL REQUIRED)

include_directories("glfw/include")

set( ENKITS_BUILD_EXAMPLES OFF CACHE BOOL "Build basic example applications" )
set( GLFW_BUILD_EXAMPLES OFF CACHE BOOL  "GLFW lib only" )
set( GLFW_BUILD_TESTS OFF CACHE BOOL  "GLFW lib only" )
set( GLFW_BUILD_DOCS OFF CACHE BOOL  "GLFW lib only" )
set( GLFW_BUILD_INSTALL OFF CACHE BOOL  "GLFW lib only" )

add_subdirectory( glfw )

target_link_libraries( executablename glfw ${GLFW_LIBRARIES} )

@Vinnie Your documentation URL is specific for version 3.0, which is fairly old. Use the latest alias to get the documentation for the most recent release: http://www.glfw.org/docs/latest/build.html

However, half of the relevant section seems to have gone missing. Below are the missing bits:

You don’t need any GLFW_ variables anymore; all necessary dependencies and paths are now stored in the imported glfw CMake target you get from the glfw3 config-file package. Just add glfw as a dependency for your program.

target_link_libraries(myapp glfw)

@dougbinks This also applies to source tree builds. You shouldn’t need the GLFW_ dependency variables anymore either.

Edit: You shouldn’t need include_directories for the GLFW headers either, as that’s also part of the glfw target.

2 Likes

@elmindreda Thanks, I had a quick read through the generated glfw3Target.cmake file. I did not realize you could set the include directories as a property of the target! Great tip for future projects.

My mistake was in trying to get things working sequentially in my new CMake project, i.e. tell CMake to find the headers before / without specifying the target_link_libraries(myapp glfw), thus when the compiler failed to find the headers, it send me down the GLFW_INCLUDE_DIRS rabbit hole. Thanks for the clarification! It’s working beautifully! :slight_smile:

1 Like

Dear Vinnie
Please let me suggest you article How to Use CMake to Get Binaries from Source Code.
With regards and friendship
Georges Theodosiou