# How to have CMake resolve directories

**URL:** https://discourse.cmake.org/t/how-to-have-cmake-resolve-directories/4209
**Category:** Usage
**Created:** [October 5, 2021, 2:15pm UTC](https://discourse.cmake.org/t/how-to-have-cmake-resolve-directories/4209 "2021-10-05T14:15:51Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![theoremseeker](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/ac91a4/32.png) [@theoremseeker](https://discourse.cmake.org/u/theoremseeker)
#### Post date: [October 5, 2021, 2:15pm UTC](https://discourse.cmake.org/t/how-to-have-cmake-resolve-directories/4209/1 "2021-10-05T14:15:51Z")

</div>

Currently, I have to add directories to my files in my C/C++ project both in the CMakeLists.txt and my include directives, for example:

add\_library (library1  
src/dir1/file1.h  
src/dir1/file1.c  
)

and in file1.c itself:

#include “…/dir2/fileNeededByFile1.h”

etc.

I’d like CMake to resolve this for me automatically. I thought that the “include\_directories” would do this, but it seems to not be the case. How can I tell CMake to look into certain directories (recursively, or if not possible, not recursively) and avoid having to write the directories one by one?

---

<div class="post-metadata">

### Author: ![buildSystemPerson](https://discourse.cmake.org/user_avatar/discourse.cmake.org/buildsystemperson/32/2851_2.png) [@buildSystemPerson](https://discourse.cmake.org/u/buildSystemPerson)
#### Post date: [October 5, 2021, 2:56pm UTC](https://discourse.cmake.org/t/how-to-have-cmake-resolve-directories/4209/2 "2021-10-05T14:56:16Z")

</div>

I’m really unclear on what you are asking for.

This article does a pretty good job explaining include directories with CMake though.

> **[Adding C++ Header Include Directories With CMake](https://matgomes.com/header-include-directories-cmake/)**
>
> Adding header include directories is straightforward with CMake. Learn how to tell the compiler where to find those pesky C++ header files with simple commands!Continue readingAdding C++ Header Include Directories With CMake

---

<div class="post-metadata">

### Author: ![theoremseeker](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/ac91a4/32.png) [@theoremseeker](https://discourse.cmake.org/u/theoremseeker)
#### Post date: [October 5, 2021, 3:18pm UTC](https://discourse.cmake.org/t/how-to-have-cmake-resolve-directories/4209/3 "2021-10-05T15:18:15Z")

</div>

Thanks for the link, I will check it.

What I want is to not have to write the directory of every file both in the add\_library add\_executable etc. commands and the include directories in the code, as I surmise that CMake likely offers me a way to solve this. That is, instead of

add\_library (library1  
src/dir1/file1.h  
src/dir1/file1.c  
src/dir2/file2.h  
src/dir2/file2.c  
…  
)

I’d like to be able to write:

add\_library (library1  
file1.h  
file1.c  
file2.h  
file2.c  
)

and in file1.h’s directives, instead of  
#include “…/src/dir3/fileNeededByFile1.h”.

I’d like to write  
#include “fileNeededByFile1.h”.

I expect that there is a

command\_to\_set\_directories( library1  
src/dir1/  
src/dir2/  
src/dir3/  
}  
to do that.

---

<div class="post-metadata">

### Author: ![buildSystemPerson](https://discourse.cmake.org/user_avatar/discourse.cmake.org/buildsystemperson/32/2851_2.png) [@buildSystemPerson](https://discourse.cmake.org/u/buildSystemPerson)
#### Post date: [October 5, 2021, 6:41pm UTC](https://discourse.cmake.org/t/how-to-have-cmake-resolve-directories/4209/4 "2021-10-05T18:41:31Z")

</div>

There isn’t a CMake command that auto resolves directories the way you want.

Your best tools for adding files and include directories are `target_sources` / `target_include_directories`

I’d look at this [blog post by Craig Scott](https://crascit.com/2016/01/31/enhanced-source-file-handling-with-target_sources/) for handling your source files in a nice way which I have seen accepted in many projects that want to make their CMake code nicer.

Essentially your CMake script would end up looking like this:

```auto
# CMakeLists.txt
cmake_minimum_required(VERSION 3.13)

project(FOOBAR LANGUAGES C)

add_library(library)
add_subdirectory(src)
add_subdirectory(inc)

```

Overall I think that’s a nice presentation 👍

Then you have a CMakeLists.txt in src that looks like this (You can obviously do this for as many directories as you want):

```auto
# src/CMakeLists.txt
target_include_directories(library PRIVATE .)
target_sources(library PRIVATE 
     file1.c 
     file1.h
     file2.c
     file2.h
)

```

And potentially if you a nice library author your CMakeLists.txt in your inc directory looks like this:

```auto
# inc/CMakeLists.txt

# Notice the use of PUBLIC!
# Now when a client links against your library they automatically absorb the inc/ include directory
# Which makes their CMake code nicer since they don't need to know how your project is laid out!
target_include_directories(library PUBLIC .)
target_sources(library PRIVATE
    library1.h
)

```

---

<div class="post-metadata">

### Author: ![theoremseeker](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/ac91a4/32.png) [@theoremseeker](https://discourse.cmake.org/u/theoremseeker)
#### Post date: [October 11, 2021, 5:28am UTC](https://discourse.cmake.org/t/how-to-have-cmake-resolve-directories/4209/5 "2021-10-11T05:28:05Z")

</div>

That looks nice. Thank you so much.
