# Issues adding GLFW source to a CMake project

**URL:** <https://discourse.glfw.org/t/issues-adding-glfw-source-to-a-cmake-project/1511>\
**Category:** support\
**Created:** [March 22, 2020, 2:13pm UTC](https://discourse.glfw.org/t/issues-adding-glfw-source-to-a-cmake-project/1511 "2020-03-22T14:13:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tachi](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/tachi/32/316_2.png) [@Tachi](https://discourse.glfw.org/u/Tachi)\
**Post date:** [March 22, 2020, 2:13pm UTC](https://discourse.glfw.org/t/issues-adding-glfw-source-to-a-cmake-project/1511/1 "2020-03-22T14:13:35Z")

</div>

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:

```auto
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:

```auto
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

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [March 22, 2020, 2:51pm UTC](https://discourse.glfw.org/t/issues-adding-glfw-source-to-a-cmake-project/1511/2 "2020-03-22T14:51:21Z")

</div>

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

> **[juliettef/GLFW-CMake-starter](https://github.com/juliettef/GLFW-CMake-starter)**
>
> Use CMake to create a project with GLFW - Multi-platform Windows, Linux and MacOS. - juliettef/GLFW-CMake-starter

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 )`.

---

<div class="post-metadata">

**Author:** ![Tachi](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/tachi/32/316_2.png) [@Tachi](https://discourse.glfw.org/u/Tachi)\
**Post date:** [March 22, 2020, 3:16pm UTC](https://discourse.glfw.org/t/issues-adding-glfw-source-to-a-cmake-project/1511/3 "2020-03-22T15:16:29Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [March 22, 2020, 3:31pm UTC](https://discourse.glfw.org/t/issues-adding-glfw-source-to-a-cmake-project/1511/4 "2020-03-22T15:31:05Z")

</div>

Could you first try using the project from [https://github.com/juliettef/GLFW-CMake-starter](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.

---

<div class="post-metadata">

**Author:** ![Tachi](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/tachi/32/316_2.png) [@Tachi](https://discourse.glfw.org/u/Tachi)\
**Post date:** [March 22, 2020, 4:51pm UTC](https://discourse.glfw.org/t/issues-adding-glfw-source-to-a-cmake-project/1511/5 "2020-03-22T16:51:36Z")

</div>

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](http://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

```auto
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!

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [March 22, 2020, 4:54pm UTC](https://discourse.glfw.org/t/issues-adding-glfw-source-to-a-cmake-project/1511/6 "2020-03-22T16:54:25Z")

</div>

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}`.

---

<div class="post-metadata">

**Author:** ![Tachi](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/tachi/32/316_2.png) [@Tachi](https://discourse.glfw.org/u/Tachi)\
**Post date:** [March 22, 2020, 5:03pm UTC](https://discourse.glfw.org/t/issues-adding-glfw-source-to-a-cmake-project/1511/7 "2020-03-22T17:03:55Z")

</div>

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