# CMake target\_include\_directories doesn't export to compile\_commands.json when using CUDA

**URL:** https://discourse.cmake.org/t/cmake-target-include-directories-doesnt-export-to-compile-commands-json-when-using-cuda/10072
**Category:** Usage
**Tags:** lang:cuda
**Created:** [February 12, 2024, 9:55pm UTC](https://discourse.cmake.org/t/cmake-target-include-directories-doesnt-export-to-compile-commands-json-when-using-cuda/10072 "2024-02-12T21:55:17Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![SidYaj](https://discourse.cmake.org/user_avatar/discourse.cmake.org/sidyaj/32/4304_2.png) [@SidYaj](https://discourse.cmake.org/u/SidYaj)
#### Post date: [February 12, 2024, 9:55pm UTC](https://discourse.cmake.org/t/cmake-target-include-directories-doesnt-export-to-compile-commands-json-when-using-cuda/10072/1 "2024-02-12T21:55:17Z")

</div>

I am trying to use CMake to build a CUDA project. However, when I use add\_library followed by target\_include\_directories, the relevant commands are not updated in the compile\_commands.json file.  
 ![Screenshot 2024-02-12 at 21.45.05](https://discourse.cmake.org/uploads/default/original/2X/8/8b27cbd430d3a5d98e9eb166c39ac7cd0a63c7dc.png)  
The structure of the project is shown here. There are three CMakeLists.txt; one in Euler\_CUDA, one in src and one in solver. In sequence, the CMakeLists are the following:

```auto
cmake_minimum_required(VERSION 3.19)

project(CUDA_Practice)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wall -Werror -pedantic")
enable_language(CUDA)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)

add_subdirectory(src)

execute_process(
    COMMAND ${CMAKE_COMMAND} -E create_symlink
        ${CMAKE_BINARY_DIR}/compile_commands.json
        ${CMAKE_SOURCE_DIR}/compile_commands.json
)

```

```auto
add_subdirectory(solver)

find_package(PkgConfig REQUIRED)
pkg_search_module(LIBCONFIG++ REQUIRED libconfig++)

add_executable(main main.cu)
target_include_directories(main PUBLIC ${LIBCONGIG++_INCLUDE_DIRS})

target_link_libraries(main PUBLIC solver_lib)
target_link_libraries(main PUBLIC ${LIBCONFIG++_LINK_LIBRARES})

```

```auto
add_library(solver_lib STATIC solver.cu equationOfState.cu)

target_include_directories(solver_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)

```

And the compile\_commands.json file which is output looks like the following

```auto
[
{
  "directory": "/home/raid/sy451/Euler_CUDA/build/src",
  "command": "/lsc/opt/cuda-11.7/bin/nvcc -forward-unknown-to-host-compiler --options-file CMakeFiles/main.dir/includes_CUDA.rsp -std=c++17 \"--generate-code=arch=compute_52,code=[compute_52,sm_52]\" -x cu -c /home/raid/sy451/Euler_CUDA/src/main.cu -o CMakeFiles/main.dir/main.cu.o",
  "file": "/home/raid/sy451/Euler_CUDA/src/main.cu",
  "output": "src/CMakeFiles/main.dir/main.cu.o"
},
{
  "directory": "/home/raid/sy451/Euler_CUDA/build/src/solver",
  "command": "/lsc/opt/cuda-11.7/bin/nvcc -forward-unknown-to-host-compiler --options-file CMakeFiles/solver_lib.dir/includes_CUDA.rsp -std=c++17 \"--generate-code=arch=compute_52,code=[compute_52,sm_52]\" -x cu -c /home/raid/sy451/Euler_CUDA/src/solver/solver.cu -o CMakeFiles/solver_lib.dir/solver.cu.o",
  "file": "/home/raid/sy451/Euler_CUDA/src/solver/solver.cu",
  "output": "src/solver/CMakeFiles/solver_lib.dir/solver.cu.o"
},
{
  "directory": "/home/raid/sy451/Euler_CUDA/build/src/solver",
  "command": "/lsc/opt/cuda-11.7/bin/nvcc -forward-unknown-to-host-compiler --options-file CMakeFiles/solver_lib.dir/includes_CUDA.rsp -std=c++17 \"--generate-code=arch=compute_52,code=[compute_52,sm_52]\" -x cu -c /home/raid/sy451/Euler_CUDA/src/solver/equationOfState.cu -o CMakeFiles/solver_lib.dir/equationOfState.cu.o",
  "file": "/home/raid/sy451/Euler_CUDA/src/solver/equationOfState.cu",
  "output": "src/solver/CMakeFiles/solver_lib.dir/equationOfState.cu.o"
}
]

```

As you can see, there is no -I flag with the include directory mentioned anywhere. However, when I create an analogous C++ project, it works fine, leading me to believe that the problem is with CMake and CUDA. The project compiles fine but it poses a problem when I use clangd. My temporary solution is to manually add the include flags to the compile\_commands.json but it would be nice if the flag could be explicitly output.

---

<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: [February 17, 2024, 6:24am UTC](https://discourse.cmake.org/t/cmake-target-include-directories-doesnt-export-to-compile-commands-json-when-using-cuda/10072/2 "2024-02-17T06:24:35Z")

</div>

> [@SidYaj](#):
>
> `target_include_directories(main PUBLIC ${LIBCONGIG++_INCLUDE_DIRS})`

Did you perhaps mean `LIBCONFIG++_INCLUDE_DIRS` here?

---

<div class="post-metadata">

### Author: ![tuero](https://discourse.cmake.org/user_avatar/discourse.cmake.org/tuero/32/4343_2.png) [@tuero](https://discourse.cmake.org/u/tuero)
#### Post date: [February 25, 2024, 10:46am UTC](https://discourse.cmake.org/t/cmake-target-include-directories-doesnt-export-to-compile-commands-json-when-using-cuda/10072/3 "2024-02-25T10:46:02Z")

</div>

I’m getting the same behaviour. Any -I flag is not being forwarded to the compiler flags for nvcc, but is for regular C++ compilation. I do see that for nvcc, the `compile_commands.json` has a `--options-file CMakelists/<>.rsp` entry, whose file does have the include flags, but LSPs like `clangd` can’t read that.

I guess my question is, why are the include flags being placed into the `*.rsp` file instead of directly into the `compile_commands.json` for the cuda targets, but not regular C++ targets?

---

<div class="post-metadata">

### Author: ![SidYaj](https://discourse.cmake.org/user_avatar/discourse.cmake.org/sidyaj/32/4304_2.png) [@SidYaj](https://discourse.cmake.org/u/SidYaj)
#### Post date: [February 25, 2024, 9:22pm UTC](https://discourse.cmake.org/t/cmake-target-include-directories-doesnt-export-to-compile-commands-json-when-using-cuda/10072/4 "2024-02-25T21:22:36Z")

</div>

Yes, that’s what I meant!

---

<div class="post-metadata">

### Author: ![SidYaj](https://discourse.cmake.org/user_avatar/discourse.cmake.org/sidyaj/32/4304_2.png) [@SidYaj](https://discourse.cmake.org/u/SidYaj)
#### Post date: [February 25, 2024, 9:23pm UTC](https://discourse.cmake.org/t/cmake-target-include-directories-doesnt-export-to-compile-commands-json-when-using-cuda/10072/5 "2024-02-25T21:23:15Z")

</div>

Yeah I believe that is the case. One solution is to add these extra flags to the .clangd file.

---

<div class="post-metadata">

### Author: ![tuero](https://discourse.cmake.org/user_avatar/discourse.cmake.org/tuero/32/4343_2.png) [@tuero](https://discourse.cmake.org/u/tuero)
#### Post date: [February 25, 2024, 9:39pm UTC](https://discourse.cmake.org/t/cmake-target-include-directories-doesnt-export-to-compile-commands-json-when-using-cuda/10072/6 "2024-02-25T21:39:07Z")

</div>

From this issue here, it appears that there was a regression with clangd and reading the response files: [clangd 17 regression: response files are not expanded when using command inference · Issue #69690 · llvm/llvm-project · GitHub](https://github.com/llvm/llvm-project/issues/69690)

It appears that there was a way to not use the response files and have the include flags directly into the compile command by using `-DCMAKE_CXX_USE_RESPONSE_FILE_FOR_INCLUDES=OFF`, but I have not had success with that.

---

<div class="post-metadata">

### Author: ![SidYaj](https://discourse.cmake.org/user_avatar/discourse.cmake.org/sidyaj/32/4304_2.png) [@SidYaj](https://discourse.cmake.org/u/SidYaj)
#### Post date: [February 25, 2024, 11:00pm UTC](https://discourse.cmake.org/t/cmake-target-include-directories-doesnt-export-to-compile-commands-json-when-using-cuda/10072/7 "2024-02-25T23:00:10Z")

</div>

Based on what I read, it seems that this problem is likely to be fixed in clangd 18.

---

<div class="post-metadata">

### Author: ![twmht](https://discourse.cmake.org/user_avatar/discourse.cmake.org/twmht/32/4374_2.png) [@twmht](https://discourse.cmake.org/u/twmht)
#### Post date: [March 5, 2024, 2:50am UTC](https://discourse.cmake.org/t/cmake-target-include-directories-doesnt-export-to-compile-commands-json-when-using-cuda/10072/8 "2024-03-05T02:50:32Z")

</div>

any update on this?

---

<div class="post-metadata">

### Author: ![Chao\_Qu](https://discourse.cmake.org/user_avatar/discourse.cmake.org/chao_qu/32/4592_2.png) [@Chao\_Qu](https://discourse.cmake.org/u/Chao_Qu)
#### Post date: [May 13, 2024, 6:34am UTC](https://discourse.cmake.org/t/cmake-target-include-directories-doesnt-export-to-compile-commands-json-when-using-cuda/10072/9 "2024-05-13T06:34:38Z")

</div>

I’m having the same issue and I’m using clangd 18.1.3.

---

<div class="post-metadata">

### Author: ![Chao\_Qu](https://discourse.cmake.org/user_avatar/discourse.cmake.org/chao_qu/32/4592_2.png) [@Chao\_Qu](https://discourse.cmake.org/u/Chao_Qu)
#### Post date: [May 13, 2024, 6:50am UTC](https://discourse.cmake.org/t/cmake-target-include-directories-doesnt-export-to-compile-commands-json-when-using-cuda/10072/10 "2024-05-13T06:50:09Z")

</div>

What worked for me is adding the following to my cmakelists

```auto
set(CMAKE_CUDA_USE_RESPONSE_FILE_FOR_INCLUDES 0)
set(CMAKE_CUDA_USE_RESPONSE_FILE_FOR_LIBRARIES 0)
set(CMAKE_CUDA_USE_RESPONSE_FILE_FOR_OBJECTS 0)

```

---

<div class="post-metadata">

### Author: ![Syple\_Black](https://discourse.cmake.org/user_avatar/discourse.cmake.org/syple_black/32/4724_2.png) [@Syple\_Black](https://discourse.cmake.org/u/Syple_Black)
#### Post date: [July 5, 2024, 3:32am UTC](https://discourse.cmake.org/t/cmake-target-include-directories-doesnt-export-to-compile-commands-json-when-using-cuda/10072/11 "2024-07-05T03:32:28Z")

</div>

try to edit /usr/share/cmake/Modules/Compiler/NVIDIA.cmake, delete --option-files and RESPONSE\_FILE content at the end of the file. it works for me

 ![image](https://discourse.cmake.org/uploads/default/original/2X/4/4fdee821872fbb79938f825ecd927b1381da0c32.png)

---

<div class="post-metadata">

### Author: ![icyveins7](https://discourse.cmake.org/user_avatar/discourse.cmake.org/icyveins7/32/5371_2.png) [@icyveins7](https://discourse.cmake.org/u/icyveins7)
#### Post date: [February 27, 2025, 7:07am UTC](https://discourse.cmake.org/t/cmake-target-include-directories-doesnt-export-to-compile-commands-json-when-using-cuda/10072/12 "2025-02-27T07:07:34Z")

</div>

> What worked for me is adding the following to my cmakelists

```auto
set(CMAKE_CUDA_USE_RESPONSE_FILE_FOR_INCLUDES 0)
set(CMAKE_CUDA_USE_RESPONSE_FILE_FOR_LIBRARIES 0)
set(CMAKE_CUDA_USE_RESPONSE_FILE_FOR_OBJECTS 0)

```

This worked! Thanks for this. clangd properly parses things again!
