# target\_sources() and target\_include\_directories() behaviour

**URL:** https://discourse.cmake.org/t/target-sources-and-target-include-directories-behaviour/15297
**Category:** Code
**Tags:** gen:makefiles
**Created:** [November 12, 2025, 2:23pm UTC](https://discourse.cmake.org/t/target-sources-and-target-include-directories-behaviour/15297 "2025-11-12T14:23:53Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![moonsun\_interstell](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@moonsun\_interstell](https://discourse.cmake.org/u/moonsun_interstell)
#### Post date: [November 12, 2025, 2:23pm UTC](https://discourse.cmake.org/t/target-sources-and-target-include-directories-behaviour/15297/1 "2025-11-12T14:23:54Z")

</div>

i am not sure about how cmake retrieves header files. (see cmake code below)  
my expectation is that it looks only what is defined in target\_include\_directories().

According to the cmake text file and the directory structure i would expect a compiler error since the path to “DummyTest\_prv.h” is not explicitly described.  
but compiling/linking goes through as it seems DummyTest\_prv.h is discovered “somehow” by CMake.

Since the build system is Unix Makefile, i found under build/CMakeFiles/myapp.dir/src/DummyTest\_prv.c.o.d:  
CMakeFiles/myapp.dir/src/DummyTest\_prv.c.o:  
/home/yof1dy/private/test/rbDyDummyTest/src/DummyTest\_prv.c  
/usr/include/stdc-predef.h  
**/home/yof1dy/private/test/rbDyDummyTest/src/DummyTest\_prv.h**

is it intended like this? i couldn’t find any documentation.

Thanks CMake Team.

cmake code below

* * *

# folder structure

#.

#├── CMakeLists.txt

#├── inc

#│ └── DummyTest.h

#└── src

# ├── DummyTest.c

# ├── DummyTest\_prv.c

# └── DummyTest\_prv.h

# set minimum cmake version

cmake\_minimum\_required(VERSION 3.5 FATAL\_ERROR)

# project name and language

project(Helloworld LANGUAGES C)

add\_executable(myapp)

target\_include\_directories(myapp

PUBLIC

${CMAKE\_CURRENT\_SOURCE\_DIR}/inc/

)

target\_sources(myapp

PRIVATE

${CMAKE\_CURRENT\_SOURCE\_DIR}/src/DummyTest.c

${CMAKE\_CURRENT\_SOURCE\_DIR}/src/DummyTest\_prv.c

)

target\_compile\_options(myapp

PUBLIC

-Wall

)

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [November 12, 2025, 2:34pm UTC](https://discourse.cmake.org/t/target-sources-and-target-include-directories-behaviour/15297/2 "2025-11-12T14:34:08Z")

</div>

It’s not discovered by CMake, but by the compiler (CMake asks the compiler for the dependencies to create those “\*.d” files).

So this is a consequence of header search rules implemented by the compiler. Specifically, the compiler also searches the directory where the including file is. GCC even prefers this if you use `#include "header.h"` (so if you use quotes as opposed to angle brackets).

---

<div class="post-metadata">

### Author: ![moonsun\_interstell](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@moonsun\_interstell](https://discourse.cmake.org/u/moonsun_interstell)
#### Post date: [November 12, 2025, 2:46pm UTC](https://discourse.cmake.org/t/target-sources-and-target-include-directories-behaviour/15297/3 "2025-11-12T14:46:09Z")

</div>

Thanks for the quick answer.  
when you say:  
”Specifically, the compiler also searches the directory where the including file is.”  
–\> do you mean the compiler searches the header files in the folder where the src file is located?

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [November 12, 2025, 3:17pm UTC](https://discourse.cmake.org/t/target-sources-and-target-include-directories-behaviour/15297/4 "2025-11-12T15:17:58Z")

</div>

Yes.  
Look at the documentation (gcc-specific): [Search Path (The C Preprocessor)](https://gcc.gnu.org/onlinedocs/cpp/Search-Path.html)
