CUDA_NVCC_FLAGS is being ignored in CMake

Hi there, I am pretty new to building my own CUDA program with CMake, and I want to build a very simple project. Which just has one cu file, and I want to be able to debug it in VSCode (or cuda-dbg, which I have used in project developed by others before)

I have read through some of the simpler CUDA sample code and know that in order to enable the debug mode in both CPU and GPU, the nvcc compiler has to be invoked with “-g -G” flag. And indeed that works for the sample codes that came with the CUDA toolkit (of which they uses makefile directly)

I have read through the forum and stackoverflow about CUDA using CMake, and they all point to passing the CUDA_NVCC_FLAGS, which I have done so in the CMakeLists.txt

cmake_minimum_required(VERSION 3.8)
project(mmul_tiled CUDA CXX)
enable_language(CUDA)
set(CUDA_VERBOSE_BUILD ON)
find_package(CUDA REQUIRED)

set(CUDA_NVCC_FLAGS "-G;-g;")

add_executable(
    mmul_cached_tiles
    mmul_source.cu)
    
set_target_properties(mmul_cached_tiles PROPERTIES CUDA_SEPARABLE_COMPILATION ON)

And invoked the following command in the ‘build’ directory:

cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CUDA_COMPILER=/usr/local/cuda-11.4/bin/nvcc ..

And then invoke the make command:

make VERBOSE=1

But i have observed that from the log, the nvcc compiler being called with just the ‘-g’ flag, instead of both ‘-g -G’, thus I can only debug the CPU code but not the GPU code:

[ 33%] Building CUDA object CMakeFiles/mmul_cached_tiles.dir/mmul_source.cu.o
/usr/local/cuda-11.4/bin/nvcc -forward-unknown-to-host-compiler   -g -std=c++14 -MD -MT CMakeFiles/mmul_cached_tiles.dir/mmul_source.cu.o -MF CMakeFiles/mmul_cached_tiles.dir/mmul_source.cu.o.d -x cu -dc "/home/jefftam/JTDev/cuda_course_linux/03 mmul_cached_tiles/mmul_source.cu" -o CMakeFiles/mmul_cached_tiles.dir/mmul_source.cu.o
[ 66%] Linking CUDA device code CMakeFiles/mmul_cached_tiles.dir/cmake_device_link.o
/opt/cmake-3.21.2-linux-x86_64/bin/cmake -E cmake_link_script CMakeFiles/mmul_cached_tiles.dir/dlink.txt --verbose=1
/usr/local/cuda-11.4/bin/nvcc -forward-unknown-to-host-compiler -g -Xcompiler=-fPIC -Wno-deprecated-gpu-targets -shared -dlink CMakeFiles/mmul_cached_tiles.dir/mmul_source.cu.o -o CMakeFiles/mmul_cached_tiles.dir/cmake_device_link.o  -lcudadevrt -lcudart_static -lrt -lpthread -ldl 
[100%] Linking CUDA executable mmul_cached_tiles
/opt/cmake-3.21.2-linux-x86_64/bin/cmake -E cmake_link_script CMakeFiles/mmul_cached_tiles.dir/link.txt --verbose=1
/usr/bin/g++ CMakeFiles/mmul_cached_tiles.dir/mmul_source.cu.o CMakeFiles/mmul_cached_tiles.dir/cmake_device_link.o -o mmul_cached_tiles  -lcudadevrt -lcudart_static -lrt -lpthread -ldl  -L"/usr/local/cuda-11.4/targets/x86_64-linux/lib/stubs" -L"/usr/local/cuda-11.4/targets/x86_64-linux/lib"

Attached the CMakeLists.txt, CMakError.log and CMakeOutput.log and the copy of the Make log:

CMakeLists.txt (734 Bytes)
CMakeError.log (3.2 KB)
CMakeOutput.log (83.6 KB)
MakeVerbose.log (3.3 KB)

Appreciate any help at all, been stuck on this for a while.

Best,
JT

this may be of interest. That was the first item google returned for me when I did a search for CUDA_NVCC_FLAGS.

hi Thank you for the answer. I have tried this

#  CMakeLists.txt to build mmul_cached_tiles.cu
cmake_minimum_required(VERSION 3.8)
project(mmul_tiled CUDA CXX)
enable_language(CUDA)
set(CUDA_VERBOSE_BUILD ON)
find_package(CUDA REQUIRED)

add_executable(
    mmul_cached_tiles
    mmul_source.cu)

target_compile_options(mmul_cached_tiles PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:
                       --generate-line-info
                       --use_fast_math
                       --relocatable-device-code=true
                       >
                       $<$<CUDA_NVCC_FLAGS>: 
                       "-g -G"
                       >
                       )
                       
set_target_properties(mmul_cached_tiles PROPERTIES CUDA_SEPARABLE_COMPILATION ON)

And some variation of the ‘generator’ logic, but doesn’t seem to want to compile the CMakeLists file. I am not familiar with. Is it possible to give me a bit of a pointer?

Also has the (set(xxx)) functionality been phased out?

I figured out a way to get around this, so that it can do CPU/GPU computing
I think the flag itself can’t be controlled (or is overwritten later), so noticing there is a flag called “CMAKE_CUDA_FLAGS_DEBUG” from Robert Maynard’s presentation. I tried setting it as such. And voila I can now do CPU/GPU debugging.

Though, if someone know of the proper way to do this (switch on the debug flags) please do let me know,

my CMakeLists.txt is now:

#  CMakeLists.txt to build mmul_cached_tiles.cu
cmake_minimum_required(VERSION 3.8)
project(mmul_tiled CUDA CXX)
enable_language(CUDA)
set(CUDA_VERBOSE_BUILD ON)
find_package(CUDA REQUIRED)


# shared library of the cuda should be static
# provide the cuda compiler: cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CUDA_COMPILER=/usr/local/cuda-11.4/bin/nvcc ..
set(CMAKE_CUDA_FLAGS_DEBUG "-g -G")

add_executable(
    mmul_cached_tiles
    mmul_source.cu)

                       
set_target_properties(mmul_cached_tiles PROPERTIES CUDA_SEPARABLE_COMPILATION ON)

And then let the CMake know it is debug mode:

cmake -DCMAKE_BUILD_TYPE=Debug ..