# Library and include path

**URL:** <https://discourse.cmake.org/t/library-and-include-path/9523>\
**Category:** Code\
**Created:** [November 28, 2023, 4:01pm UTC](https://discourse.cmake.org/t/library-and-include-path/9523 "2023-11-28T16:01:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oodini](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.cmake.org/u/Oodini)\
**Post date:** [November 28, 2023, 4:01pm UTC](https://discourse.cmake.org/t/library-and-include-path/9523/1 "2023-11-28T16:01:06Z")

</div>

Hello,

I am creating a library, and the associated test.

Here is the files structure :

```auto
── MyLibrary
   ├── CMakeLists.txt
   ├── include
   │ └── x.h
   ├── src
   │ ├── CMakeLists.txt
   │ ├── x.cpp
   │ ├── y.h
   │ └── y.cpp
   └── test
       ├── CMakeLists.txt
       ├── test.h
       └── test.cpp

```

and where is what I do in the CMakeLists.txt inside the tesy directory :

```auto
target_sources(${LIB_PROJECT_NAME}
               PUBLIC
                   test.h
                   test.cpp
			   PUBLIC
				   ../include/x.h)
				   
source_group(TREE . PREFIX Sources)

target_include_directories(${TEST_PROJECT_NAME}
                           PUBLIC
						       ${CMAKE_CURRENT_SOURCE_DIR}
							   ${LIB_PROJECT_NAME}/include/)

```

The problem is that in the test, if I want to include a.h, I just have to do :

`#include <a.h>`

I’d like to be able to do :

`#include <MyLibrary/a.h>`

I read several posts on StackOverflow telling to do :

```auto
── MyLibrary
   ├── CMakeLists.txt
   ├── include
   │ └── MyLibrary
   │ └── x.h

```

It’s not very clean, and when I am including a.h in the library, I have also to add MyLibrary in the path, where I am already in the library.

Would it be possible to alias the include directory so that

`${LIB_PROJECT_NAME}/include/`

becomes

`MyLibrary/`

?

PS : I am not sure of the PUBLIC access in the commands. Let me know if I should change anything about this point.

---

<div class="post-metadata">

**Author:** ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)\
**Post date:** [November 29, 2023, 1:17am UTC](https://discourse.cmake.org/t/library-and-include-path/9523/2 "2023-11-29T01:17:43Z")

</div>

The path in the `#include` is interpreted by the compiler. It searches in the filesystem for that path.  
There’s nothing CMake or any other build system can do about that.

---

<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 29, 2023, 7:36am UTC](https://discourse.cmake.org/t/library-and-include-path/9523/3 "2023-11-29T07:36:55Z")

</div>

I’ve learned to use that pattern (include/MyLibrary) and now it seems pretty natural to me.  
I set up the library target to use `include` and `include/MyLibrary` (typically the first is public/interface, and the second is private), and just roll with that.  
It looked weird at first, but after all it seems to be the cleanest solution.

---

<div class="post-metadata">

**Author:** ![Oodini](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.cmake.org/u/Oodini)\
**Post date:** [November 29, 2023, 9:43am UTC](https://discourse.cmake.org/t/library-and-include-path/9523/4 "2023-11-29T09:43:48Z")

</div>

OK, so I made up my mind to follow this “canonical way”. 🙂  
Thanks.
