I have been trying to set up GLFW to start an OpenGL project, but an error keeps appearing that I cannot get to solve, no matter what I try. The important part of the error message is the following :
CMake Error at CMakeLists.txt:12 (find_package):
Could not find a configuration file for package "glfw3" that is compatible
with requested version "3.3.2".
The following configuration files were considered but not accepted:
C:/Users/../GLFW/lib/cmake/glfw3/glfw3Config.cmake, version: 3.3.2 (32bit)
My CMakeLists.txt looks like this :
set(CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/Dependencies/GLFW/lib/cmake/glfw3")
add_executable(1_Intro main.cpp)
include_directories(Dependencies/GLFW/include/GLFW/)
find_package(glfw3 3.3.2 CONFIG REQUIRED)
target_link_libraries(1_Intro ${GLFW})
and all the necessary GLFW files can be found in my project's folder like so :
GLFW
|- include
| |- GLFW
| | - glfw3.h
| | - glfw3native.h
|- lib
|- cmake
|- glfw3
|- glfw3Config.cmake
|- glfw3ConfigVersion.cmake
|- glfw3Targets.cmake
|- glfw3Targets-noconfig.cmake
|- pkgconfig
|- glfw3.pc
|- libglfw3.a
What I did to build the library was downloading the source code, then using cmake -G "MinGW Makefiles"
as I use the MinGW-w64 GCC compiler, then mingw32-make
and mingw32-make install
. Then I copied the build files to the folder like shown above.
After searching for a solution, I found that the problem might come from a difference in the build architecture used by the library and the compiler (here and here). I use the MinGW-w64 compiler and would prefer to stick with it, so I tried changing the CMake build. However, I came across another problem, that I cannot change the platform specifications of "MinGW Makefiles" with options like -DCMAKE_GENERATOR_PLATFORM
. I tried changing the toolchain but that didn't work either; the only solution seems to be to use the Visual Studio 64 bits toolchain, but that's not what I want.
Is there a way to change my CMake build in such a way that it is a x64 architecture ? Or to set my compiler to x86 (if that even makes sense) to match the "glfw3Config.cmake" version ? I am open to other solutions, as long as I can keep my project structure and the compiler as they are.
So I figured out what the problem was. I had on my computer both MinGW-w64 and MinGW, and both of their bin
directory paths in PATH. When running where gcc
, for example, the latter bin
path was being shown, but not the former. So I deleted MinGW's paths from PATH, restarted the computer, re-built and re-compiled as explained before, and now it works.
I am making this as an answer, though, to explain the steps I took to install GLFW, which was a bit of a nightmare and where I encountered many problems before the one described above. I use Windows 10, CMake 3.17.2, MinGW-w64 with GCC 8.1.0, but the following works the same with other compilers and architectures, with the necessary changes :
Downloading the source code from the GLFW website. As of writing this, the version is 3.3.2.
Building the library :
First, unzip the source code and create a build
folder inside of it (though you could create it outside of the source folder, there is no difference) and run cmake -G "MinGW Makefiles" -S . -B build
(I recommend running where gcc
before, to check if the GCC path is the one pointing to MinGW-w64's bin
; see this post)
Run in the build
folder the command mingw32-make
. This will take a little long, as it will make all the targets, not only the library itself but documentation and examples too. If you don't want this, you can return to step 2.1 and run the CMake command with options as described here (a simpler option is to just use the CMake GUI, where you can turn on and off these options fairly easily). I would like to thank the author of this post, whose precise description of the build process (and the fact that he was installing GLFW with CMake, on Windows, like me) helped me a great deal.
You should have a cmake_install.cmake file. Open it : you should see in the first few lines something like this :
# Set the install prefix
if(NOT DEFINED CMAKE_INSTALL_PREFIX)
set(CMAKE_INSTALL_PREFIX "C:/Program Files (x86)/GLFW")
endif()
This is the default CMAKE_INSTALL_PREFIX path. It tells the make program where to install the dependencies : change it to the folder you want, but keep in mind that it won't be created, it must already exist (and don't forget to change the backslashes \
to slashes /
). Now run mingw32-make install
. You should see in the folder you gave an include
and lib
folders.
Editing the CMakeLists.txt :
Now that you have built your library, you need to edit your CMakeLists.txt file in order to link GLFW to your CMake project. A typical unmodified CMakeLists.txt file looks like this :
cmake_minimum_required(VERSION 3.17)
project(PROJECT_NAME)
set(CMAKE_CXX_STANDARD 17)
add_executable(PROJECT_NAME main.cpp)
What you need to link is first set the CMAKE_PREFIX_PATH variable. In my case, I added this line before add_executable(..)
:
set(CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/Dependencies/GLFW/lib/cmake/glfw3")
This is because I wanted to have the GLFW dependencies in my project's folder. However, it should work fine if you put an absolute path too: what is important is that the folder contains the glfw3Config.cmake and glfw3Targets.cmake files (if you built correctly the library, you should find them under build_folder/lib/cmake/glfw3/
).
Now that you have told CMake where to look for the glfw3Config.cmake file, you need to link the libraries to the project. You can do this by adding after the add_executable(..)
command :
find_package(glfw3 REQUIRED)
target_link_libraries(PROJECT_NAME glfw)
If that gives you an error like
By not providing "Findglfw3.cmake" in CMAKE_MODULE_PATH this project
has asked CMake to find a package configuration file provided by "glfw3", but
CMake did not find one.
Could not find a package configuration file provided by "glfw3" (requested
version 3.2) with any of the following names:
glfw3Config.cmake
glfw3-config.cmake
Add the installation prefix of "glfw3" to CMAKE_PREFIX_PATH or set
"glfw3_DIR" to a directory containing one of the above files. If "glfw3"
provides a separate development package or SDK, be sure it has been installed.
add CONFIG
between glfw3
and REQUIRED
(see here for details).
The last thing you need to do is adding the line include_directories(Dependencies/GLFW/include/GLFW/)
with a path to include
in the GLFW build folder. The CMakeLists.txt should look like this now :
cmake_minimum_required(VERSION 3.17)
project(PROJECT_NAME)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/Dependencies/GLFW/lib/cmake/glfw3")
add_executable(PROJECT_NAME main.cpp)
include_directories(Dependencies/GLFW/include/GLFW/)
find_package(glfw3 REQUIRED)
target_link_libraries(PROJECT_NAME glfw)
If you got here without errors, congratulations. You have succesfully linked GLFW to your CMake project ! Now a final thing to get your project up and running and be able to write some basic OpenGL code : change the find_package
part to this :
find_package(OpenGL REQUIRED)
find_package(glfw3 REQUIRED)
target_link_libraries(PROJECT_NAME opengl32 glfw)
As you may have supposed, this will link OpenGL to the project. There is no need to do anything more than this, as the OpenGL DLL (opengl32.dll and glu32.dll) should already be in C:\Windows\System32
.
I hope this answer will help people to install GLFW without having to do a day worth of forum QA scrolling, documentation reading and Youtube tutorials watching.