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.

Thanks for your example. I ended up with something similar, but I ran into a few issues. The one I am currently trying to resolve, is this error when I compile my program:

C:\Developer\myApp\glfw\src\window.c(37): fatal error C1083: Cannot open include file: 'Window.h': No such file or directory

I tried using different compilers, re-wrote the CMake code, but can’t get past this. I even re-installed VS and updated it to make sure I had the Windows SDK but with no luck. I feel like I will have to find the path to Windows.h and include it manually, but why would I have to? I have compiled GLFW on its own before with no problem. What’s different this time why it can’t find it?

Edit: I guess I will say that my other problem was, that somehow one of the CMake files in GLFW was forcing my whole program to compile with the C compiler regardless of my .cpp files. The only fix I could get was to not use GLFW, and so I just resolved it by making this project only C. I am not sure if this will help with the other problem, but I figured I just say it anyway.

Edit 2: If needed, I may just ask this as a separate question as I had already resolved this one.

Where did you get glfw source?
Line 37 in window.c does not contain “Window.h” include.

Have you modified it with extra changes yourself?

Huh I am pretty sure I just downloaded from the website. I will try re-adding the most latest release from GitHub.

Well that was the problem. My guess is that I pressed some key combination in CLion that added that line because it thought I needed to include that.