Search code examples
c++cmakestatic-librariesglfw

Can't compile Cmake project external library ( using GLWF (OpenGL))


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)

Here you can see my project tree: project tree

Do you have any idea of what's wrong?


Solution

  • You do this:

    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) # <- this line
    

    I'm pretty sure you should be using INTERFACE_INCLUDE_DIRECTORIES (which is for include directories added to targets that link to the target the property is specified on) instead of INCLUDE_DIRECTORIES (which is for the target the property is specified on). Using INCLUDE_DIRECTORIES for an imported target actually doesn't make any sense, since it's imported- it's already built.

    Note that you can also set include directories by using the target_include_directories command. Ex.

    target_include_directories(GLFW INTERFACE "${CMAKE_SOURCE_DIR}/external")