# Add cpp files from sub directories in target\_sources or not

**URL:** https://discourse.cmake.org/t/add-cpp-files-from-sub-directories-in-target-sources-or-not/13708
**Category:** Code
**Created:** [March 10, 2025, 4:42pm UTC](https://discourse.cmake.org/t/add-cpp-files-from-sub-directories-in-target-sources-or-not/13708 "2025-03-10T16:42:13Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![JVApen](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jvapen/32/2152_2.png) [@JVApen](https://discourse.cmake.org/u/JVApen)
#### Post date: [March 10, 2025, 4:42pm UTC](https://discourse.cmake.org/t/add-cpp-files-from-sub-directories-in-target-sources-or-not/13708/1 "2025-03-10T16:42:13Z")

</div>

We are moving from some build system to CMake for a large project.  
We have everything working, though we are now cleaning up some stuff.

Currently we have something like:

```auto
target_sources(target_o PRIVATE
    very/long/path/to/file.cpp
    path/to/file.cpp
    path/to/file2.cpp
    ...)

```

Note that this code is currently generated as it is easier to maintain together with the old build system.

Though, on some projects like LLVM, you rather see something like:

```auto
add_subdirectory(very)
add_subdirectory(path)

```

This than recurses through the folder structure and it ends up with:

```auto
target_sources(target_o PRIVATE file.cpp file2.cpp)

```

I see advantages to both approaches, like having 1 place where all sources of a target are known in the first version. The second version makes renaming directories much easier.

Is there a recommended way to detail with many cpp files in a directory structure and adding them to targets? If not, what are the most important arguments for either approach?

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [March 12, 2025, 8:23pm UTC](https://discourse.cmake.org/t/add-cpp-files-from-sub-directories-in-target-sources-or-not/13708/2 "2025-03-12T20:23:46Z")

</div>

My advice is generally to make each `CMakeLists.txt` responsible for things in the same directory as itself, and delegate things in subdirectories to the `CMakeLists.txt` files in those subdirectories. Avoid pulling up lists of files from subdirectories to their parents. This has a few advantages:

- Whenever you’re adding or removing files, you know that the `CMakeLists.txt` file you need to update is in the same directory as those files. You don’t have to go searching up the parent directory hierarchy to find which `CMakeLists.txt` file is adding them.
- It is easier to move directories around if you need to do that at some point, since each directory tends to be relatively self-contained.
- Each `CMakeLists.txt` tends to be simpler and more focused.
- If you need to conditionally add things to the build, this structure tends to facilitate that by allowing you to just conditionally add a subdirectory. This is especially helpful when you have platform-specific subdirectories.
