# Usage of target\_sources() does not work

**URL:** https://discourse.cmake.org/t/usage-of-target-sources-does-not-work/9157
**Category:** Usage
**Created:** [October 9, 2023, 2:55pm UTC](https://discourse.cmake.org/t/usage-of-target-sources-does-not-work/9157 "2023-10-09T14:55:27Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ChristophR](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/258eb7/32.png) [@ChristophR](https://discourse.cmake.org/u/ChristophR)
#### Post date: [October 9, 2023, 2:55pm UTC](https://discourse.cmake.org/t/usage-of-target-sources-does-not-work/9157/1 "2023-10-09T14:55:27Z")

</div>

Hello all,

I am new to cmake and therefore sorry in advance if I missed the solution to my problem in this forum.

Like many people, I have a project consisting of sub-directories with C files. I read that _add\_subdirectory_ ( \<sub\_dir\> ) combined with _target\_sources_ ( file\_list ) is the appropriate choise.

My problem is that I cannot use _target\_sources_ in the sub-dir’s CMakeLists.txt without getting

```auto
CMake Error at sub_dir/CMakeLists.txt:3 (target_sources):
  Cannot specify sources for target "mylib" which is not built by this project.

```

**CMakeList.txt in root**

```auto
cmake_minimum_required(VERSION 3.25.3 )
project( test )
include_directories ( some_dir )
add_subdirectory ( subdir )
add_library( mylib root.c dummy.c )

```

**CMakeList.txt in sub\_dir**

`target_sources(mylib PRIVATE some_file.c )`

My CMake is a 3.25.3 on Cygwin. Once I succeed I intend to move to SOLARIS.

What am I missing ?

Christoph

---

<div class="post-metadata">

### Author: ![robert.maynard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robert.maynard/32/4_2.png) [@robert.maynard](https://discourse.cmake.org/u/robert.maynard)
#### Post date: [October 9, 2023, 7:41pm UTC](https://discourse.cmake.org/t/usage-of-target-sources-does-not-work/9157/2 "2023-10-09T19:41:25Z")

</div>

The call of `add_library( mylib ...` needs to occur before `add_subdirectory`. You can only call `target_sources` after the associated target has been created.

---

<div class="post-metadata">

### Author: ![ChristophR](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/258eb7/32.png) [@ChristophR](https://discourse.cmake.org/u/ChristophR)
#### Post date: [October 10, 2023, 9:26am UTC](https://discourse.cmake.org/t/usage-of-target-sources-does-not-work/9157/3 "2023-10-10T09:26:36Z")

</div>

Hello Rob,

thanks for reply.

I figured out that my **target name** has to be equal to the **project name** for cmake to work (which is quite puzzling me).  
My project is an application which runs on two linked embedded computers (redundancy) in parallel with slight differences in IO structure. My thought was having a project name and two targets, one for computer A and one for computer B. The common part can as well be a library, but for the hardware specific part the plan was to link common lib with specific files and main() - containing file.

Having Project and target name tied together is not working for me.

Can some briefly describe how I could have one project, two targets (i.e.) executables?

Christoph

---

<div class="post-metadata">

### Author: ![robert.maynard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robert.maynard/32/4_2.png) [@robert.maynard](https://discourse.cmake.org/u/robert.maynard)
#### Post date: [October 10, 2023, 1:26pm UTC](https://discourse.cmake.org/t/usage-of-target-sources-does-not-work/9157/4 "2023-10-10T13:26:32Z")

</div>

```cmake
cmake_minimum_required(VERSION 3.20)
project(example)

add_library(shared_bits SHARED)
target_sources(shared_bits PRIVATE s.cpp)

add_executable(first)
target_sources(first PRIVATE main.cpp)
target_link_libraries(first PRIVATE shared_bits)

add_executable(second)
target_sources(second PRIVATE another_main.cpp)
target_link_libraries(second PRIVATE shared_bits)
```

---

<div class="post-metadata">

### Author: ![ChristophR](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/258eb7/32.png) [@ChristophR](https://discourse.cmake.org/u/ChristophR)
#### Post date: [October 10, 2023, 3:57pm UTC](https://discourse.cmake.org/t/usage-of-target-sources-does-not-work/9157/5 "2023-10-10T15:57:51Z")

</div>

Thanks, helped a lot …

C
