# App Build with cmake mingw

**URL:** <https://discourse.glfw.org/t/app-build-with-cmake-mingw/1176>\
**Category:** support\
**Created:** [September 24, 2018, 1:24am UTC](https://discourse.glfw.org/t/app-build-with-cmake-mingw/1176 "2018-09-24T01:24:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RedSray](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/redsray/32/198_2.png) [@RedSray](https://discourse.glfw.org/u/RedSray)\
**Post date:** [September 24, 2018, 1:24am UTC](https://discourse.glfw.org/t/app-build-with-cmake-mingw/1176/1 "2018-09-24T01:24:18Z")

</div>

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)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake/modules/")

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

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](https://github.com/JoeyDeVries/LearnOpenGL/blob/master/cmake/modules/FindGLFW3.cmake) 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

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [September 24, 2018, 10:31am UTC](https://discourse.glfw.org/t/app-build-with-cmake-mingw/1176/2 "2018-09-24T10:31:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![RedSray](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/redsray/32/198_2.png) [@RedSray](https://discourse.glfw.org/u/RedSray)\
**Post date:** [September 24, 2018, 10:30pm UTC](https://discourse.glfw.org/t/app-build-with-cmake-mingw/1176/3 "2018-09-24T22:30:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [September 25, 2018, 9:16am UTC](https://discourse.glfw.org/t/app-build-with-cmake-mingw/1176/4 "2018-09-25T09:16:41Z")

</div>

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