# include directories not passed to compiler despite INCLUDE\_DIRECTORIES

**URL:** https://discourse.cmake.org/t/include-directories-not-passed-to-compiler-despite-include-directories/8971
**Category:** Usage
**Created:** [September 14, 2023, 8:40am UTC](https://discourse.cmake.org/t/include-directories-not-passed-to-compiler-despite-include-directories/8971 "2023-09-14T08:40:26Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![f-zl](https://discourse.cmake.org/user_avatar/discourse.cmake.org/f-zl/32/3824_2.png) [@f-zl](https://discourse.cmake.org/u/f-zl)
#### Post date: [September 14, 2023, 8:40am UTC](https://discourse.cmake.org/t/include-directories-not-passed-to-compiler-despite-include-directories/8971/1 "2023-09-14T08:40:26Z")

</div>

I have a cross compile project. I wish to use `include_directories(SYSTEM ${ISYSTEM_PATH})` to add the system include path for all targets (including targets in `add_subdirectory`), but fails.

The INCLUDE\_DIRECTORIES is set correctly for the target, but the path doesn’t show up when invoking the compiler.

```cmake
# top CMakeLists
cmake_minimum_required(VERSION 3.16)
set(CMAKE_TOOLCHAIN_FILE ./toolchain.cmake) # toolchain is detected correctly
set(ISYSTEM_PATH somePath)
add_compile_options(-mcpu=cortex-a9 -mfpu=vfpv3 -mfloat-abi=hard) # compile option for all targets, which is correctly passed to the compiler
include_directories(SYSTEM ${ISYSTEM_PATH}) # include path for all targets, which is not passed to the compiler

project(someProject LANGUAGES C)
add_subdirectory(shared)
# other subdirectories

```

```cmake
# one of the subdirectory
project(shared LANGUAGES C)
add_library(${PROJECT_NAME} STATIC
    shared.c
)
# display target includes
get_target_property(sharedIncludes ${PROJECT_NAME} INCLUDE_DIRECTORIES)
message("shared target includes")
message("[")
foreach(dir ${dirIncludes})
    message(STATUS "${dir}")
endforeach()
message("]")

```

The message shows the shared target includes has `somePath` from the top CMakeLists. But the include path is not passed to the compiler. Where does it go wrong? Thanks.

CMake version 3.27.4. The compiler is arm-none-eabi-gcc.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [September 14, 2023, 9:00pm UTC](https://discourse.cmake.org/t/include-directories-not-passed-to-compiler-despite-include-directories/8971/2 "2023-09-14T21:00:20Z")

</div>

What is the real value you’re setting `ISYSTEM_PATH` to? Would that path possibly be one of the default paths the compiler already searches? If so, I think CMake will remove it from the command line, since it is already searched by the compiler by default.

---

<div class="post-metadata">

### Author: ![f-zl](https://discourse.cmake.org/user_avatar/discourse.cmake.org/f-zl/32/3824_2.png) [@f-zl](https://discourse.cmake.org/u/f-zl)
#### Post date: [September 15, 2023, 12:29am UTC](https://discourse.cmake.org/t/include-directories-not-passed-to-compiler-despite-include-directories/8971/3 "2023-09-15T00:29:08Z")

</div>

ISYSTEM\_PATH is the compiler’s default paths indeed. I add that so that the path will be in the exported compile\_commands.json, so that clang based tools (clangd) can see the system path.

---

<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: [September 15, 2023, 1:17am UTC](https://discourse.cmake.org/t/include-directories-not-passed-to-compiler-despite-include-directories/8971/4 "2023-09-15T01:17:55Z")

</div>

> [@f-zl](#):
>
> I add that so that the path will be in the exported compile\_commands.json, so that clang based tools (clangd) can see the system path.

Tools that read `compile_commands.json` have to understand the tool used in the command line there (this will only get worse with C++ modules as CMake will only generate modules for the actual compiler and `clang-tidy`, `clangd`, etc. aren’t likely to learn GCC’s module format anytime soon to do the `import` when doing its thing). I’d recommend using a Clang build of the project for such use cases. Or teach Clang how to understand the compiler being used more natively (e.g., how it does so when given a GCC command line). Cross-compiles seem to need more work here.

---

<div class="post-metadata">

### Author: ![f-zl](https://discourse.cmake.org/user_avatar/discourse.cmake.org/f-zl/32/3824_2.png) [@f-zl](https://discourse.cmake.org/u/f-zl)
#### Post date: [September 15, 2023, 2:48am UTC](https://discourse.cmake.org/t/include-directories-not-passed-to-compiler-despite-include-directories/8971/5 "2023-09-15T02:48:51Z")

</div>

Thanks for the replies. The workaround I get is add -isystem as compiler flags in `add_compile_options`.

```cmake
# top CMakeLists
set(ISYSTEM_PATH somePath)
list(TRANSFORM ISYSTEM_PATH REPLACE ".+" "SHELL:-isystem \\0" OUTPUT_VARIABLE ISYSTEM_FLAGS)
# ISYSTEM_FLAGS should be "SHELL:-isystem path1";"SHELL:-isystem path2"

add_compile_options(${ISYSTEM_FLAGS}) # replace include_directories(SYSTEM ${ISYSTEM_PATH})

```
