Search code examples
c++cmakecudaclion

Code parsing not working with CUDA, Clion and CMake


I have a project divided in modules, here is a dummy example:

  • root
    • CMakeLists.txt
    • modules
      • utils
        • CMakeLists.txt
        • src
          • util_file.cpp
      • cuda
        • CMakeLists.txt
        • src
          • cuda_file.cu

If I edit the cuda_file.cu with CLion, all the symbols are unresolved (even the includes from standard library) by CLion. All the code completion/creation features are then of course gone (among other things). The problem seems to be that whenever you create a library or an executable with only CUDA files, Clion becomes stupid and doesn't parse or resolve anything anymore.

There is two workarounds I've found but they are not friendly or "clean" to use :

  • add an empty .cpp file to the directory and add it to the add_library() CMake line.
  • switch to another library or executable target that has .cpp files (like utils in my dummy example). But then when you want to compile or execute you have to switch again to cuda target (or some subtarget like test_cuda for test units) and then switch back again to continue coding or debugging, etc...

Here is the CMakeLists.txt from the cuda module with the workaround:

cmake_minimum_required(VERSION 3.5)
message(STATUS "Configuring module cuda")

# Build module static library
FILE(GLOB CUDA_SRCS
    ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
FILE(GLOB CUDA_CU_SRCS
     ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cu)
FILE(GLOB CUDA_CU_HDRS
    ${CMAKE_CURRENT_SOURCE_DIR}/include/*.cuh)

cuda_compile(cuda_objs ${CUDA_CU_SRCS} ${CUDA_CU_HDRS})
add_library(cuda STATIC ${CUDA_SRCS} ${cuda_objs})
# because only .cu files, help cmake detect C++ language
set_target_properties(cuda PROPERTIES LINKER_LANGUAGE CXX)

Is there a way to avoid CLion derping when resolving links to other headers and libraries ?

I've already added .cu and .cuh files as C/C++ code in CLion options and tried using JETBRAINS_IDE define option as explained in another similar post, but those two problems are not the same.


Solution

  • As of the version 2020.1 of CLion, CUDA projects are now officially supported

    https://blog.jetbrains.com/clion/2020/04/clion-2020-1-cuda-clang-embedded/