# Beginner: Ways of including source subfolders?

**URL:** <https://discourse.cmake.org/t/beginner-ways-of-including-source-subfolders/9773>\
**Category:** Code\
**Created:** [January 4, 2024, 11:00pm UTC](https://discourse.cmake.org/t/beginner-ways-of-including-source-subfolders/9773 "2024-01-04T23:00:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![SparkyNZ](https://discourse.cmake.org/user_avatar/discourse.cmake.org/sparkynz/32/3930_2.png) [@SparkyNZ](https://discourse.cmake.org/u/SparkyNZ)\
**Post date:** [January 4, 2024, 11:00pm UTC](https://discourse.cmake.org/t/beginner-ways-of-including-source-subfolders/9773/1 "2024-01-04T23:00:38Z")

</div>

Can CMake be used to dynamically pull in source file folders? I’m not talking about using libraries per se.

Say for example I have a top level CMakeLists.txt file. I know I can explicitly include subfolder content like this:

```auto
file(GLOB other_SRCS 
    other_src/*.cpp
)
add_executable(MyTarget ${other_SRCS})

```

But how can I tell the top CMakeLists.txt to include “whatever” is in the **other\_src** folder as defined by an **other\_src/CMakeLists.txt**?

Would I do it like this?  
`include(${CMAKE_CURRENT_SOURCE_DIR}/other_src/CMakeLists.txt)`

Now the bit I’m struggling with - if I don’t want to generate a library, what would I put in the **other\_src/CMakeLists.txt** file?

```auto
file(GLOB other_SRCS 
    *.cpp
)

what else?

```

---

<div class="post-metadata">

**Author:** ![dnglaze](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/74df32/32.png) [@dnglaze](https://discourse.cmake.org/u/dnglaze)\
**Post date:** [January 5, 2024, 4:15am UTC](https://discourse.cmake.org/t/beginner-ways-of-including-source-subfolders/9773/2 "2024-01-05T04:15:31Z")

</div>

The important function you are looking for is [target\_sources](https://cmake.org/cmake/help/latest/command/target_sources.html).

Top level CMakeLists.txt would have something like

```cmake
add_executable(MyTarget)
add_subdirectory(other_src)

```

Content in the `other_src` CMakeLists.txt

```cmake
file(GLOB other_SRCS *.cpp)
target_sources(MyTarget PRIVATE ${other_SRC})

```

I try to use `add_subdirectory` if I’m referencing a CMakeLists.txt and use `include` when I’m pulling in a `*.cmake` file. There is a difference between `include` and `add_subdirectory`, so it would depend on your use case which would be better.

As an aside, be aware of the pitfalls of `file(GLOB)` for defining source files. See [file glob](https://cmake.org/cmake/help/latest/command/file.html#glob) documentation. Be sure to understand the tradeoffs when trying to use it that way.

---

<div class="post-metadata">

**Author:** ![SparkyNZ](https://discourse.cmake.org/user_avatar/discourse.cmake.org/sparkynz/32/3930_2.png) [@SparkyNZ](https://discourse.cmake.org/u/SparkyNZ)\
**Post date:** [January 5, 2024, 5:25am UTC](https://discourse.cmake.org/t/beginner-ways-of-including-source-subfolders/9773/3 "2024-01-05T05:25:37Z")

</div>

@dnglaze thanks for that - would “MyTarget” in target\_sources() be “OtherSourcesTarget” - ie - a target name local to the other\_SRC files?

---

<div class="post-metadata">

**Author:** ![dnglaze](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/74df32/32.png) [@dnglaze](https://discourse.cmake.org/u/dnglaze)\
**Post date:** [January 5, 2024, 5:32am UTC](https://discourse.cmake.org/t/beginner-ways-of-including-source-subfolders/9773/4 "2024-01-05T05:32:30Z")

</div>

In this situation where you aren’t looking to create another library, you can just use the target defined in the parent CMakeLists.txt, the executable `MyTarget` in this case. As long as the target has been defined, you can add additional sources to it with `target_sources`. So no, there’s no local target you have to worry about.

You likely _could_ define a separate target to contain the sources if you wanted, but I would imagine that’s only interesting in situations where you want to apply those sources to multiple targets. This is likely more complicated than what you’re looking for, but leverages the same `target_sources` command.

If instead you are looking to organize files and are using a generator that supports filters, such as Visual Studio, you can always use a `source_group` to group stuff in `other_src` into a separate filter.

---

<div class="post-metadata">

**Author:** ![SparkyNZ](https://discourse.cmake.org/user_avatar/discourse.cmake.org/sparkynz/32/3930_2.png) [@SparkyNZ](https://discourse.cmake.org/u/SparkyNZ)\
**Post date:** [January 5, 2024, 8:02pm UTC](https://discourse.cmake.org/t/beginner-ways-of-including-source-subfolders/9773/5 "2024-01-05T20:02:00Z")

</div>

Thanks @dnglaze . This should help me a little in accepting what the “normal” way would be for doing what we’re talking about.

I used to use Visual Studio 2010 (and earlier) all the time so yes, the concept of a filter (or “folder” as I preferred to call them) made sense under the main source tree. So source\_group may be worth reading about.

It’s actually or the IDF ESP32 environment that I’m wanting to understand CMake with. IDF has a few concepts of it’s own (they call them “components”) but I figured it would be best that I understand vanilla CMake first - especially when I see it being used increasingly in all aspects of work and hobbies that I’m exposed to these days.

---

<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:** [January 5, 2024, 9:26pm UTC](https://discourse.cmake.org/t/beginner-ways-of-including-source-subfolders/9773/6 "2024-01-05T21:26:18Z")

</div>

You may find my blog post on `target_sources()` to be a helpful read. It covers similar use cases to yours. [Enhanced source file handling with target\_sources() - Crascit](https://crascit.com/2016/01/31/enhanced-source-file-handling-with-target_sources/)

---

<div class="post-metadata">

**Author:** ![charMstr](https://discourse.cmake.org/user_avatar/discourse.cmake.org/charmstr/32/4952_2.png) [@charMstr](https://discourse.cmake.org/u/charMstr)\
**Post date:** [September 20, 2024, 6:46pm UTC](https://discourse.cmake.org/t/beginner-ways-of-including-source-subfolders/9773/7 "2024-09-20T18:46:43Z")

</div>

beware: the answer of @dnglaze has just a little bug: the ${other\_SRC} is missing an “S” at the end, it should be: ${other\_SRCS} to match the variable declaration on the previous line.  
😉
