Issues adding GLFW source to a CMake project

I’m a new one here, so hi everyone! : )

I’m having a lot of issues with building GLFW from source in my CMake project.
I’ve added the source from the “latest” branch as a git submodule in a vendor folder.
I’ve read the docs on the GLFW site, but I get a linking error when building with my project; here is the CMakeError.log:

ld.lld: error: undefined symbol: pthread_create
>>> referenced by ld-temp.o
>>>               lto.tmp:(main)

ld.lld: error: undefined symbol: pthread_detach
>>> referenced by ld-temp.o
>>>               lto.tmp:(main)

ld.lld: error: undefined symbol: pthread_join
>>> referenced by ld-temp.o
>>>               lto.tmp:(main)
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I also tried to compile and link with GCC and ld, but the problem remains.

The strange thing is that when building just GLFW (using CMake) I don’t get any errors.

If it can help, here is the GLFW-related part of my CMakeLists.txt:

set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
add_subdirectory(vendor/glfw)
target_link_libraries(${PROJECT_NAME} glfw)

How could I fix the issue?

Thanks

We’ve made an example cmake starter project for GLFW on Github:

Feel free to take a look at the CMakeLists.txt file and use that for help. It looks like you’re missing the ${GLFW_LIBRARIES} variable from your target_link_libraies, this is a list of all the libraries required by GLFW for your system, you will likely also need ${OPENGL_LIBRARIES} which is created by find_package( OpenGL REQUIRED ).

Hi Doug, thanks for the fast reply.
I’ve modified my CMakeLists.txt as you suggested, but I still get the same error.
Here’s my full CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)

project(CppSnake)

set(HEADER_FILES Apple.hpp IndexBuffer.hpp Quad.hpp Random.hpp Renderer.hpp Shader.hpp Snake.hpp Vertex.hpp VertexArray.hpp VertexBuffer.hpp VertexBufferLayout.hpp)

add_executable(${PROJECT_NAME} main.cpp ${HEADER_FILES})

target_include_directories(${PROJECT_NAME} vendor/glad/include vendor/glfw/include)

add_subdirectory(vendor/glad)
target_link_libraries(${PROJECT_NAME} glad)
target_link_libraries(${PROJECT_NAME} dl)

set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
find_package(OpenGL REQUIRED)
add_subdirectory(vendor/glfw)
target_link_libraries(${PROJECT_NAME} glfw ${OPENGL_LIBRARIES} ${GLFW_LIBRARIES})

target_link_libraries(${PROJECT_NAME} GL)

I really don’t understand why it is complaining about pthread things

Could you first try using the project from https://github.com/juliettef/GLFW-CMake-starter and then if that works for you it should be possible for you to build up your own project by referencing that one.

Turns out the problem was the order I wrote things.
I had to first add GLFW’s subdirectory, and then do everything else, except for including GLFW’s include directory, a thing that its CMake file does automatically.
The sad part is that on glfw.org/docs/latest/build.html I couldn’t find any info about this : /
Maybe it’s just because I’m inexperienced, but I think that the GLFW team should add more info about this in the docs, it could save a lot of time for people like me :sweat_smile:

Here is my updated CMakeLists.txt, in case somebody will come across the same issue

cmake_minimum_required(VERSION 3.5)

project(CppSnake)

set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "GLFW lib only")
set(GLFW_BUILD_TESTS OFF CACHE BOOL "GLFW lib only")
set(GLFW_BUILD_DOCS OFF CACHE BOOL "GLFW lib only")
set(GLFW_BUILD_INSTALL OFF CACHE BOOL "GLFW lib only")
add_subdirectory(vendor/glfw)
add_subdirectory(vendor/glad)

set(HEADER_FILES Apple.hpp IndexBuffer.hpp Quad.hpp Random.hpp Renderer.hpp Shader.hpp Snake.hpp Vertex.hpp VertexArray.hpp VertexBuffer.hpp VertexBufferLayout.hpp)
add_executable(${PROJECT_NAME} main.cpp ${HEADER_FILES})

target_link_libraries(${PROJECT_NAME} glad glfw ${OPENGL_LIBRARIES} ${GLFW_LIBRARIES})

Thanks again to @dougbinks and all the GLFW team!

Note that I see you use target_link_libraries multiple times for the same project in the original version. I do not know if this is supported by cmake and so using one target_link_libraries for all libraries you want to link is best - if you need to you can concatenate them into a variable then use that.

Your current cmake adds dl and GL directly, so won’t work cross platform. Hence our starter uses ${OPENGL_LIBRARIES} and ${GLFW_LIBRARIES}.

Oh, I see, you’re right. I’ll edit my CMake file and my previous post. Thanks again!