Can't add GLFW mingw64 static library to CMake project

Hello,

I’m trying to compile a project to start learning OpenGl, but I can’t.

When I run CMake -G "Unix Makefiles" .. it generates the MakeFile without any warning. When running Make, it fails to compile. Here’s the error raised.

fatal error: GLFW/glfw3.h: No such file or directory
    2 | #include <GLFW/glfw3.h>

Here is my CMakeLists.txt where I added the library.

cmake_minimum_required(VERSION 3.9.1)
project(TEST)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

add_library(GLFW STATIC IMPORTED GLOBAL)
set_target_properties(GLFW PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/external/GLFW/libglfw3.a )
set_target_properties(GLFW PROPERTIES INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR}/external)

add_executable(hello ${CMAKE_SOURCE_DIR}/src/main.cpp)

target_link_libraries(hello GLFW)

My project tree:

root\
      external\
                   GLFW\
                             glfw3.h
                             libglfw3.a
      src\
            main.cpp
      build\
      CMakeLists.txt

Do you have any idea of what’s wrong? Thanks ^^’

It looks like you are setting the include directory on the GLFW target, whereas it should be set on the hello target as this is the one which includes the glfw3.h header. Alternatively I think you can set the interface_include_directories on the GLFW target and it will be inherited by any target which links with it.

Another approach is to use the target_include_directories CMake command instead of set_target_properties. This can be inherited if PUBLIC by targets which link against the target.

You might also want to check out this CMake GLFW starter if you just want to get on with learning OpenGL.

Thank you so much. Definetly making GLFW the targe didn’t make sense. Now its targeting what it has to and it’s working

Glad to be of help and good luck learning OpenGL.

1 Like