# Is it possible to customize the include path of a file?

**URL:** https://discourse.cmake.org/t/is-it-possible-to-customize-the-include-path-of-a-file/5841
**Category:** Code
**Created:** [June 10, 2022, 6:14pm UTC](https://discourse.cmake.org/t/is-it-possible-to-customize-the-include-path-of-a-file/5841 "2022-06-10T18:14:09Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![dwarwick](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dwarwick/32/2298_2.png) [@dwarwick](https://discourse.cmake.org/u/dwarwick)
#### Post date: [June 10, 2022, 6:14pm UTC](https://discourse.cmake.org/t/is-it-possible-to-customize-the-include-path-of-a-file/5841/1 "2022-06-10T18:14:09Z")

</div>

Assume I have a directory structure like

```auto
Libs/
    Foo/
        File.h
        File.cpp
        CMakeLists.txt
    Bar/
        File2.h
        File2.cpp
        CMakeLists.txt

```

I want the include paths for files to be Foo/File.h and Bar/File2.h. However, if I do

```auto
target_include_directories(Foo ..)

```

then not only am I breaking the mental model of CMake by traversing upwards from a CMakeLists.txt, but I now also enabled including files in Bar and other targets in Libs even if I didn’t add a target dependency on them to Foo.

However, if I do

```auto
target_include_directories(Foo .)

```

then now the include is

```auto
#include "File.h"

```

which loses context as to what target the file is part of.

We have been working around this by using a directory structure of

```auto
Foo/
    include/Foo/
        Baz/
            Baz.h
        File.h
    Baz/
        Baz.cpp
    File.cpp
    CMakeLists.txt

```

which produces the desired include paths, but particularly for targets with verbose names that are deeper in the directory tree, this has pushed us over the Windows path length limit in several cases. I’d like to outright disable the path length limit, but key, external tool maintainers are dragging their feet fixing the dinosaur path length limit in their tools.

Is there a way to customize the include path (e.g. prepend it with something) of these files without actually changing the directory structure on disk?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [June 10, 2022, 8:18pm UTC](https://discourse.cmake.org/t/is-it-possible-to-customize-the-include-path-of-a-file/5841/2 "2022-06-10T20:18:52Z")

</div>

> [@dwarwick](#):
>
> Is there a way to customize the include path (e.g. prepend it with something) of these files without actually changing the directory structure on disk?

While the standard does not have anything to say about how header lookup works, just about everything actually works on filepaths. The closest you could probably get is to copy the headers into the desired layout in the build tree. However, note that compiler warnings and such will drop you into this copy and not the original if you do any followup edits.

---

<div class="post-metadata">

### Author: ![benthevining](https://discourse.cmake.org/user_avatar/discourse.cmake.org/benthevining/32/1924_2.png) [@benthevining](https://discourse.cmake.org/u/benthevining)
#### Post date: [June 10, 2022, 8:28pm UTC](https://discourse.cmake.org/t/is-it-possible-to-customize-the-include-path-of-a-file/5841/3 "2022-06-10T20:28:48Z")

</div>

You could create symlinks in the build tree?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [June 10, 2022, 8:38pm UTC](https://discourse.cmake.org/t/is-it-possible-to-customize-the-include-path-of-a-file/5841/4 "2022-06-10T20:38:59Z")

</div>

If all of your build platforms support them, sure.

---

<div class="post-metadata">

### Author: ![JRR](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@JRR](https://discourse.cmake.org/u/JRR)
#### Post date: [June 15, 2022, 9:04pm UTC](https://discourse.cmake.org/t/is-it-possible-to-customize-the-include-path-of-a-file/5841/5 "2022-06-15T21:04:42Z")

</div>

Would it work to do

```auto
target_include_directories(Libs ..)

```

when working with the assumed directory structure?

That would set the `-I` path to `..../Libs` and then your `#includes` would be

```auto
#include "Foo/File.h"
#include "Bar/File2.h"

```

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [June 16, 2022, 7:45pm UTC](https://discourse.cmake.org/t/is-it-possible-to-customize-the-include-path-of-a-file/5841/6 "2022-06-16T19:45:05Z")

</div>

I guess it would work.
