# How to gather all files automatically to compile for executable

**URL:** https://discourse.cmake.org/t/how-to-gather-all-files-automatically-to-compile-for-executable/1394
**Category:** Code
**Created:** [June 15, 2020, 12:18am UTC](https://discourse.cmake.org/t/how-to-gather-all-files-automatically-to-compile-for-executable/1394 "2020-06-15T00:18:18Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![johnratius](https://discourse.cmake.org/user_avatar/discourse.cmake.org/johnratius/32/412_2.png) [@johnratius](https://discourse.cmake.org/u/johnratius)
#### Post date: [June 15, 2020, 12:18am UTC](https://discourse.cmake.org/t/how-to-gather-all-files-automatically-to-compile-for-executable/1394/1 "2020-06-15T00:18:19Z")

</div>

I am trying to compile libraries on my own from a CMake script. For now, it’s just GLAD and GLFW.  
So for a normal project, I create a `SOURCES` variable and add the necessary headers and cpp files to it, then use the `SOURCES` variable for the sources in the `add_executable()` function.

I am wondering if I have to do this

```
list( APPEND GLAD_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/glad/glad.cpp" )
list( APPEND GLAD_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/include/glfw/glfw3.h" )

```

for every file in GLFW, GLAD, and possibly, other libraries, and add them to the `SOURCES` variable that’s used by `add_executable()`?  
I am doing this because I am trying to create my own library/framework off of GLFW and to get it compatible for most machines, the user can compile this library themselves and use the `.lib` and `.dll` files for their own use.

Is there another way to build all the source and header files of a library with my project, without needing to link any libraries (because GLFW is not compiled)?

---

<div class="post-metadata">

### Author: ![hex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hex/32/363_2.png) [@hex](https://discourse.cmake.org/u/hex)
#### Post date: [June 15, 2020, 9:50am UTC](https://discourse.cmake.org/t/how-to-gather-all-files-automatically-to-compile-for-executable/1394/2 "2020-06-15T09:50:49Z")

</div>

the build process is not entirely clear from your description, but it looks like you have external dependencies on your project. In this case you should rely on the 3rd party build procedure provided from these sources, instead of defining the set of sources and build dependencies yourself.

This can be done with the external project features from CMake.

> I am trying to compile libraries on my own from a CMake script

It looks like you already have a CMake file from that library, which makes integration easier. CMake provides a lot of default behaviour for this scenario.

Additionally to the installation, in more complex cases you would add CMake support so it can be used with your project.

---

<div class="post-metadata">

### Author: ![johnratius](https://discourse.cmake.org/user_avatar/discourse.cmake.org/johnratius/32/412_2.png) [@johnratius](https://discourse.cmake.org/u/johnratius)
#### Post date: [June 15, 2020, 2:14pm UTC](https://discourse.cmake.org/t/how-to-gather-all-files-automatically-to-compile-for-executable/1394/3 "2020-06-15T14:14:59Z")

</div>

How would I integrate this into my project? I went through the CMake files of GLFW and it wasn’t clear which I need to include.

---

<div class="post-metadata">

### Author: ![hex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hex/32/363_2.png) [@hex](https://discourse.cmake.org/u/hex)
#### Post date: [June 15, 2020, 3:56pm UTC](https://discourse.cmake.org/t/how-to-gather-all-files-automatically-to-compile-for-executable/1394/4 "2020-06-15T15:56:41Z")

</div>

The best approach is to use your libraries with `find_package( GLFW )` which the library ideally provides support for you after installation. Otherwise you should make your own CMake module such that it can be found as a package.

You can also directly consume your library by using `add_subdirectory( <GLFW_root_dir> )`. This is much easier to do.

---

<div class="post-metadata">

### Author: ![johnratius](https://discourse.cmake.org/user_avatar/discourse.cmake.org/johnratius/32/412_2.png) [@johnratius](https://discourse.cmake.org/u/johnratius)
#### Post date: [June 15, 2020, 4:11pm UTC](https://discourse.cmake.org/t/how-to-gather-all-files-automatically-to-compile-for-executable/1394/5 "2020-06-15T16:11:53Z")

</div>

Using `find_package( GLFW )` requires a `FindGLFW.cmake` which I could not find. I’ll try adding the sub directory.  
Would this mean that I have to un-separate GLFW with my project? I have combined the source and include files of GLFW into my `src` and `include` directories of my project.

---

<div class="post-metadata">

### Author: ![hex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hex/32/363_2.png) [@hex](https://discourse.cmake.org/u/hex)
#### Post date: [June 15, 2020, 4:32pm UTC](https://discourse.cmake.org/t/how-to-gather-all-files-automatically-to-compile-for-executable/1394/6 "2020-06-15T16:32:03Z")

</div>

> [@johnratius](#):
>
> Would this mean that I have to un-separate GLFW with my project? I have combined the source and include files of GLFW into my `src` and `include` directories of my project.

No, keeping your external sources separate is recommended. In your project directory structure, create a folder `extern` on root and place GLFW sources into it. Your own sources go into `src`. From your own project root CMakeLists.txt, use

```cmake
add_subdirectory(extern)
add_subdirectory(src)

```

Everytime you use `add_subdirectory` a `CMakeLists.txt` file is expected on that location. You may create additional CMakeLists.txt files to get the directory structure you want.

[https://cmake.org/cmake/help/latest/command/add\_subdirectory.html#add-subdirectory](https://cmake.org/cmake/help/latest/command/add_subdirectory.html#add-subdirectory)
