How to gather all files automatically to compile for executable

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}/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)?

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.

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.

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.

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.

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


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.