CMake target_include_directories doesn't export to compile_commands.json when using CUDA

Siddharth Yajaman
Mon, 12 Feb 2024 21:55:17 +0000
Usage

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.

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:

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
)

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})

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

[
{
  "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.

12 posts - 7 participants