Parsing doesn't work with CUDA, Clion and CMake

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

  • root
    • CMakeLists.txt
    • modules
      • Utils
        • CMakeLists.txt
        • CAC
          • util_file.cpp
      • CUDA
        • CMakeLists.txt
        • CAC
          • cuda_file.cu

If I edit cuda_file.cu with CLion, all symbols are unresolved (even including the standard library) from CLion. All completion / code generation functions then of course go away (among other things) . The problem seems to be that whenever you build a library or executable with only CUDA files, Clion becomes silly and no longer parses or resolves anything.

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

  • add an empty .cpp directory to the directory and add it to the CMake add_library () 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 need to switch back to the cuda target (or some sub-task like test_cuda for test units) and then switch back to it again to continue coding or debugging, etc.

Here is the CMakeLists.txt from the cuda module with a 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 references to other headers and libraries?

I've already added .cu and .cuh files as C / C ++ code in CLion settings and tried to use the JETBRAINS_IDE option as described in another similar post, but the two problems are not the same. ...

+3


source to share


1 answer


It seems that without Jetbrains intervening to add official CUDA support, I could exit the CLion + CMake + CUDA combo by reaching:

  • adding .cu and .cuh as C ++ files in CLion. This allows Clion to recognize cuda code as C ++ code and render it correctly.
  • adding an empty dummy.cpp file to the cuda source directory if filled with only .cu files (one of my dirty hacks from my question). I couldn't find a better one. This leaves Clion in complete control. A simple thing like recognizing cstdio doesn't work without this "hack", and CLion is basically a refined notepad.
  • using CMake 3.8+ when possible, which officially supports CUDA as a language and uses the new "add_library" other than cuda, instead of the old macro defined by cuda_add_library (). This can avoid future problems if deprecated.
  • in the CMakeLists of the cuda module (or core CMakeLists, if only one), include the path to the cuda include directory to allow the client to "see" the cuda headers. CLION may prompt you to enable them so that CLion will correctly resolve CUDA APIs such as cudaMalloc () or cudaFree (). This is only needed for CLION as the CUDA compiler does not need it to be included in the compilation (cuda.h, cuda_runtime.h, ...).
  • use this answer to create a "clion helper" header file so it doesn't affect symbols like __device__ or __global __.


I think that if Jetbrains starts adding CUDA support, it won't eliminate the need to add this dummy file either, but it will probably solve all of the things listed as well.

Here is a link to the nvidia blog with examples of official cuda language support in CMake and the new "cuda aware" add_library (): https://devblogs.nvidia.com/parallelforall/building-cuda-applications-cmake/

+2


source







All Articles