# Linking cpp files to a sub CMakeList that will compile to the Root Cmake Lists.

**URL:** <https://discourse.cmake.org/t/linking-cpp-files-to-a-sub-cmakelist-that-will-compile-to-the-root-cmake-lists/6503>\
**Category:** Code\
**Created:** [September 19, 2022, 8:44am UTC](https://discourse.cmake.org/t/linking-cpp-files-to-a-sub-cmakelist-that-will-compile-to-the-root-cmake-lists/6503 "2022-09-19T08:44:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![luke\_zerafa](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@luke\_zerafa](https://discourse.cmake.org/u/luke_zerafa)\
**Post date:** [September 19, 2022, 8:44am UTC](https://discourse.cmake.org/t/linking-cpp-files-to-a-sub-cmakelist-that-will-compile-to-the-root-cmake-lists/6503/1 "2022-09-19T08:44:11Z")

</div>

Hello there. So i have this configuration of files:

 ![image.png](https://discourse.cmake.org/uploads/default/original/2X/9/915d63186758ac9318bd6dcb90f94de9eee64618.png)

I placed the main CMakeLists.txt in the root folder, this creates an executable. In ./imgui/CMakeLists.txt I would like this CMakeList to point to specific cpp files so that root can compile the executable with them.

This is the root cmake file:

 ![image.png](https://discourse.cmake.org/uploads/default/original/2X/3/316d10c1c29c309044c0c78c5cce7517a103a8fd.png)

And currently the second one is empty. How would I be able to achieve this with cmake

---

<div class="post-metadata">

**Author:** ![mhebes](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mhebes](https://discourse.cmake.org/u/mhebes)\
**Post date:** [September 20, 2022, 12:08am UTC](https://discourse.cmake.org/t/linking-cpp-files-to-a-sub-cmakelist-that-will-compile-to-the-root-cmake-lists/6503/2 "2022-09-20T00:08:24Z")

</div>

Have you considered making the `imgui/` folder a static or object library with its own sources and then linking your top-level executable to that?

Otherwise you could just add the sources found in the subdirectory in the top-level CMakeLists. Nothing says you _must_ put a CMakeLists in every sub folder.

Alternatively you could use [`target_sources`](https://cmake.org/cmake/help/latest/command/target_sources.html) in the subdirectory (having created the initial target in the top level).

`target_sources` seems like the worst option to me though—keeping everything in the top level makes it very obvious what your exe depends on, and treating imgui/ as a separate library instead would be a great way to separate easily-testable/reusable/isolated code from your app code.
