c++openglcmakeglfw

Cannot find package GLFW using CMake


My project structure looks like this:

Two CMakeLists.txt files that deserve attention:

CMakeLists.txt

cmake_minimum_required(VERSION 3.9)
project(noam_engine)

find_package(glfw3 REQUIRED)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)

set(CMAKE_CXX_STANDARD 11)
set(NE_LIBRARIES common math graphics)

FOREACH(lib ${NE_LIBRARIES})
    add_subdirectory(src/${lib})
ENDFOREACH(lib)

add_executable(noam_engine src/main.cpp)

if(OPENGL_FOUND AND GLEW_FOUND)
    target_include_directories(noam_engine PUBLIC include ${OPENGL_INCLUDE_DIR} ${GLEW_INCLUDE_DIRS})
    target_link_libraries(noam_engine ${NE_LIBRARIES})
endif()

src/graphics/CMakeLists.txt

cmake_minimum_required(VERSION 3.9)

find_package(glfw3 REQUIRED)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)

file(GLOB SRC "*.cpp")
add_library(graphics ${SRC})

if(OPENGL_FOUND AND GLEW_FOUND)
    target_include_directories(graphics PUBLIC ${PROJECT_SOURCE_DIR}/include ${OPENGL_INCLUDE_DIR} ${GLEW_INCLUDE_DIRS})
    target_link_libraries(graphics ${OPENGL_gl_LIBRARY} ${GLFW3_LIBRARY} ${GLEW_LIBRARIES})
    message(STATUS "GLFW and GLEW successfully linked")
    message(STATUS ${OPENGL_gl_LIBRARY})
    message(STATUS ${GLFW3_LIBRARY})
    message(STATUS ${GLEW_LIBRARIES})
else()
    message(STATUS "Cannot find GL libraries")
endif()

In a few words I want to create a bunch of static libraries of the engine, in particular, link graphics library with GL's, and finally link all of them with the executable in a root CMakeLists.txt.

But I noticed that ${GLFW3_LIBRARY} is empty and I got a linker error, for example, when I call glfwInit(). I followed the guide during building and installation of GLFW

cmake .
make
make install

I believe headers and libraries are in /usr/local/*, but apparently CMake cannot find them or maybe I did something wrong.

The only hypothesis I have is that find_package doesn't know about glfw3Config.cmake which is in deps/glfw/*


Solution

  • I took the script from https://github.com/JoeyDeVries/LearnOpenGL/blob/master/cmake/modules/FindGLFW3.cmake and put it into cmake folder. Then in CMakeLists.txt I added

    list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
    

    Now everything works properly

    Thanks @Tsyvarev