App Build with cmake mingw

Hi everyone,
I try to get a simple example to build using cmake and mingw, but it seems that I can’t get nothing working…

I’ve dowloaded the glfw-3.2.1 prebuilt windows binaries and unzipped it.

Here is my CMakeLists.txt :

cmake_minimum_required(VERSION 3.0.0)
project(OpenGl_Base VERSION 0.1.0)



find_package(glfw3 3.2 REQUIRED)
find_package(OpenGL REQUIRED)

add_executable(OpenGl_Base main.cpp)
target_include_directories(OpenGl_Base PUBLIC ${OPENGL_INCLUDE_DIR} ${GLFW3_INCLUDE_DIR})
target_link_libraries(OpenGl_Base ${GLFW3_LIBRARY} ${OPENGL_gl_LIBRARY})

Note that I had to add a module for find_package to work with glfw which I copied from here which I edited to add my own path for glfw.

the error I get with this config is :

[build] Starting build

[proc] Executing command: E:\Programs\CMake\bin\cmake.EXE --build e:/Projets/OpenglBase/build --config Debug --target OpenGl_Base -- -j 6

[build] Scanning dependencies of target OpenGl_Base

[build] [ 50%] Building CXX object CMakeFiles/OpenGl_Base.dir/main.cpp.obj

[build] [100%] Linking CXX executable OpenGl_Base.exe

[build] CMakeFiles/OpenGl_Base.dir/objects.a(main.cpp.obj): In function `main':

[build] E:/Projets/OpenglBase/main.cpp:8: undefined reference to `glfwInit'

[build] E:/Projets/OpenglBase/main.cpp:12: undefined reference to `glfwCreateWindow'

[build] E:/Projets/OpenglBase/main.cpp:15: undefined reference to `glfwTerminate'

[build] E:/Projets/OpenglBase/main.cpp:20: undefined reference to `glfwMakeContextCurrent'

[build] E:/Projets/OpenglBase/main.cpp:23: undefined reference to `glfwWindowShouldClose'

[build] E:/Projets/OpenglBase/main.cpp:29: undefined reference to `glfwSwapBuffers'

[build] E:/Projets/OpenglBase/main.cpp:32: undefined reference to `glfwPollEvents'

[build] E:/Projets/OpenglBase/main.cpp:35: undefined reference to `glfwTerminate'

[build] collect2.exe: error: ld returned 1 exit status

[build] CMakeFiles/OpenGl_Base.dir/build.make:86: recipe for target 'OpenGl_Base.exe' failed

[build] make.exe[3]: *** [OpenGl_Base.exe] Error 1

[build] CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/OpenGl_Base.dir/all' failed

[build] make.exe[2]: *** [CMakeFiles/OpenGl_Base.dir/all] Error 2

[build] CMakeFiles/Makefile2:84: recipe for target 'CMakeFiles/OpenGl_Base.dir/rule' failed

[build] make.exe[1]: *** [CMakeFiles/OpenGl_Base.dir/rule] Error 2

[build] Makefile:118: recipe for target 'OpenGl_Base' failed

[build] make.exe: *** [OpenGl_Base] Error 2

[build] Build finished with exit code 2

Thanks for your time

I don’t know if the glfw find module that you’ve used works, what was the output of the cmake command?

A common issue with MinGW is that you must use a 32-bit version of GLFW if you are using a 32-bit MinGW, and a 64-bit version if you are using a 64-bit version of MinGW.

Could you check these before we explore other potential issues?

1 Like

Hi Doug,

Looks like you found the root of the problem. I was using a 32-bit mingw with 64-bit GLFW binaries.

I have been able to build my example, So the FindGLFW works properly.

I’ve first try to add those line at the begining of my CMakeLists.txt:

set(CMAKE_C_COMPILER "E:/Programs/Win-builds/bin/gcc.exe") #64bit compilers
set(CMAKE_CXX_COMPILER "E:/Programs/Win-builds/bin/g++.exe")

And it was working when using the cmake-gui. But, it was causing problems with the cmake-tools pluging I’m using in vs code.

So instead, I’ve setup the cmake-tool in vscode like this (which is like passing a -D option):

"cmake.configureSettings": {
    "CMAKE_C_COMPILER": "E:/Programs/Win-builds/bin/gcc.exe",
    "CMAKE_CXX_COMPILER": "E:/Programs/Win-builds/bin/g++.exe"

I also had to tell him explicitly to use the MinGW Makefiles generator in the config :

"cmake.generator": "MinGW Makefiles",
  "cmake.preferredGenerators": [
    "MinGW Makefiles"

Thanks a lot.

1 Like

Excellent, glad to hear that works and thanks for the cmake notes.