# Include paths not working with Shared library

**URL:** <https://discourse.cmake.org/t/include-paths-not-working-with-shared-library/12817>\
**Category:** Code\
**Tags:** os:linux, gen:ninja\
**Created:** [October 22, 2024, 10:58am UTC](https://discourse.cmake.org/t/include-paths-not-working-with-shared-library/12817 "2024-10-22T10:58:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![UserAJ](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/u/df788c/32.png) [@UserAJ](https://discourse.cmake.org/u/UserAJ)\
**Post date:** [October 22, 2024, 10:58am UTC](https://discourse.cmake.org/t/include-paths-not-working-with-shared-library/12817/1 "2024-10-22T10:58:02Z")

</div>

Hi,  
I recently got a task to move our codebase from using static builds to using shared libraries.  
I was able to do the task and the code is building and working for the executables (bar some code I commented out because cyclic dependencies were removed).

One thing that bugged me was that I had to give a longer path for including headers inside a subdirectory. I refer any directory which doesn’t have its own CMakeLists.txt as a subdirectory here.  
Basically lets assume the structure of the code is:  
src/  
 → dir1/ (has CMakeLists.txt)  
 → → subdir1.1/  
 → dir2/ (has CMakeLists.txt)  
 → → dir3/ (has CMakeLists.txt)  
 → → dir4/ (has CMakeLists.txt)  
 → → → subdir4.1/

As you may guess, the directory code uses subdir code.  
While the code was written as a Static library, I could do something like:  
#include\<subdir4.1/header.h\>  
while including a header from subdir4.1 in dir4.

With shared, I started getting errors that the file doesn’t exist.  
I was able to fix the issue by giving the path in form:  
#include\<dir2/dir4/subdir4.1/header.h\>

I’m using a GLOB\_RECURSIVE to find .cpp and .h for add\_library. Printing the variable showed that the subdir’s headers are included.  
Looking at target\_include\_directories, I tried to add the subdirectory path, but that didn’t do anything. Again printing the include\_dirs using get\_target\_property showed that the subdir path was getting appended.

I’ve tried building just dir4 (dir4 in this case is consumed by dir2) and I get the same error. Cleaning, rebuilding was tried, same for deleting CMakeCache.

Any suggestions or advice would be appreciated. Thanks

cmake version: 3.10.2  
generator: ninja  
OS: Linux

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [October 23, 2024, 5:07pm UTC](https://discourse.cmake.org/t/include-paths-not-working-with-shared-library/12817/2 "2024-10-23T17:07:27Z")

</div>

How do you set the _include\_directories_?

---

<div class="post-metadata">

**Author:** ![UserAJ](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/u/df788c/32.png) [@UserAJ](https://discourse.cmake.org/u/UserAJ)\
**Post date:** [October 25, 2024, 4:46am UTC](https://discourse.cmake.org/t/include-paths-not-working-with-shared-library/12817/3 "2024-10-25T04:46:34Z")

</div>

Hi,  
Sorry I didn’t see the reply yesterday.

```auto
target_include_directories(dir4 PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/subdir4.1
)

```

Here, please assume dir4 is a substitute for a moduleName.

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [October 25, 2024, 6:03am UTC](https://discourse.cmake.org/t/include-paths-not-working-with-shared-library/12817/4 "2024-10-25T06:03:59Z")

</div>

Ok, than every library using this needs:  
target\_link\_libraries(… dir4)

You will end in many include search paths!

I would use only src as include path.  
I guess the subdirs maps your namespaces?

---

<div class="post-metadata">

**Author:** ![UserAJ](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/u/df788c/32.png) [@UserAJ](https://discourse.cmake.org/u/UserAJ)\
**Post date:** [October 25, 2024, 8:39am UTC](https://discourse.cmake.org/t/include-paths-not-working-with-shared-library/12817/5 "2024-10-25T08:39:59Z")

</div>

Thanks for the reply.  
I’m already adding the lib to taret\_link\_library. I should have provided a proper code example. I’ll put it at the end of the comment.

**\> You will end in many include search paths!**  
I’m not sure what this means.

**\> I would use only src as include path.**  
This is what is happening right now. I’m afraid that by not allowing shorter paths, the other team members will complain. I know I found it annoying to do.

**\> I guess the subdirs maps your namespaces?**  
Not sure what you mean here. Subdirs may or may not have their own namespaces. Everything is based on what the dev making that code wanted to do at the time. No coding standards around namespaces defined.

* * *

Example CMakeLists.txt:

```auto
cmake_minimum_required (VERSION 3.10)
file(GLOB_RECURSE SRCS *.cpp *.h)

if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
add_compile_definitions(IS_WINDOWS_BUILD)
endif()

add_library(dir4 SHARED ${SRCS})

# Specify here the libraries this program depends on
target_link_libraries(dir4
    core # lets assume a module core exists
    git_version
    ${CMAKE_THREAD_LIBS_INIT}
)

# Specify here the include directories exported
# by this library
target_include_directories(dir4 PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/subdir4.1
)

```

dir2 consuming dir4 would be written as:

```auto
cmake_minimum_required (VERSION 3.10)
file(GLOB_RECURSE SRCS *.cpp *.h)

if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
add_compile_definitions(IS_WINDOWS_BUILD)
endif()

add_library(dir2 SHARED ${SRCS})

# Specify here the libraries this program depends on
target_link_libraries(dir2
    dir4
    core # lets assume a module core exists
    git_version
    ${CMAKE_THREAD_LIBS_INIT}
)

# Specify here the include directories exported
# by this library
target_include_directories(dir2 PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}
)

```

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [October 26, 2024, 2:41am UTC](https://discourse.cmake.org/t/include-paths-not-working-with-shared-library/12817/6 "2024-10-26T02:41:16Z")

</div>

If I understand right then is dir4 a subdirectory of dir2?

Than you collect the sources in subdirectory dir4 twice!

I would recommend to list the sources explicitly!
