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)
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 ).
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
Here is my updated CMakeLists.txt, in case somebody will come across the same issue
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}.