How to compile GLFW with CMake to be integrated with my project?

I basically want to make a small framework around GLFW. So I am not going to be using a compiled version of GLFW with my project. I will let users compile GLFW along with my project. Using CMake, how do I set up the compilation of GLFW, without having to make a custom CMake script that compiles each GLFW file individually? Is there a script that can all ready compile GLFW that’s built in with GLFW?

Have a look at the following starter project:

This shows how to use a cmake script to run the GLFW cmake script and link the result to your project.

Using CMake (especially properly) is an art in itself, which I can’t claim to understand, but this works for me on macOS with a folder-structure of …/my-project/libs/glfw which I have added as submodules (I left in also GLAD and GLM, all linked in slightly different ways):

cmake_minimum_required(VERSION 3.15)



add_executable(${PROJECT_NAME} ${SOURCES})

find_package(OpenGL REQUIRED)

set(GLFW_DIR ${LIB_DIR}/glfw)
set(GLFW_BUILD_EXAMPLES OFF CACHE INTERNAL "Build the GLFW example programs")
set(GLFW_BUILD_TESTS OFF CACHE INTERNAL "Build the GLFW test programs")
set(GLFW_BUILD_DOCS OFF CACHE INTERNAL "Build the GLFW documentation")
set(GLFW_INSTALL OFF CACHE INTERNAL "Generate installation target")
target_link_libraries(${PROJECT_NAME} glfw ${GLFW_LIBRARIES})
target_include_directories(${PROJECT_NAME} PRIVATE ${GLFW_DIR}/include)
target_compile_definitions(${PROJECT_NAME} PRIVATE GLFW_INCLUDE_NONE)

set(GLAD_DIR ${LIB_DIR}/glad)
add_library(glad ${GLAD_DIR}/src/glad.c)
target_include_directories(glad PRIVATE ${GLAD_DIR}/include)
target_include_directories(${PROJECT_NAME} PRIVATE ${GLAD_DIR}/include)
target_link_libraries(${PROJECT_NAME} glad ${CMAKE_DL_LIBS})

set(GLM_DIR ${LIB_DIR}/glm)

for your-sources-here you should write them like space-separated ${SRC_DIR}/filename. Make modifications according to what language you use, and always require modern versions of cmake.