Ubuntu 20环境下如何在VS Code中调试CMake构建的CUDA程序?
Hey there, let's get your CUDA debugging setup sorted out. The main issue here is that your launch.json is pointing to the source file (main.cu) instead of the compiled executable, and we need to adjust both your CMake configuration and debug launch settings to work together properly.
Step 1: Update Your CMakeLists.txt for Debugging
First, let's tweak your CMake script to ensure it generates debug-friendly binaries with CUDA debug information. Here's an improved version:
cmake_minimum_required(VERSION 3.2) project(cudaDebug CXX CUDA) # Enable debug flags for CUDA (generates symbols needed for debugging) set(CMAKE_CUDA_FLAGS_DEBUG "-G -g") # Set the CUDA architecture matching your GPU (e.g., 75 for Turing, 86 for Ampere; adjust as needed) set(CMAKE_CUDA_ARCHITECTURES 75) find_package(CUDA REQUIRED) add_executable(main main.cu)
The -G flag tells nvcc to generate debug information compatible with cuda-gdb, and specifying the architecture ensures your binary is optimized for your hardware while retaining debug symbols.
Step 2: Fix the launch.json Configuration
Your original launch.json references the source file instead of the built executable. Here's a corrected configuration that works with CMake:
{ "version": "0.2.0", "configurations": [ { "name": "CUDA C++: Launch Debug", "type": "cuda-gdb", "request": "launch", // Path to your compiled executable (adjust if using in-source build) "program": "${workspaceFolder}/build/main", // Working directory (matches where your executable runs) "cwd": "${workspaceFolder}/build", // Optional: Specify cuda-gdb path if it's not in your PATH "miDebuggerPath": "/usr/bin/cuda-gdb", // Automatically build before launching (requires a tasks.json setup) "preLaunchTask": "CMake Debug Build" } ] }
Key Fixes:
program: Points to the compiledmainexecutable (assuming you use out-of-source build in abuildfolder; if you build in the root, use${workspaceFolder}/maininstead).preLaunchTask: Ensures your project is rebuilt with debug flags every time you start debugging (we'll set this up next).
Step 3: Add a tasks.json for Automated Builds
To make debugging seamless, create a .vscode/tasks.json file to handle CMake builds in debug mode:
{ "version": "2.0.0", "tasks": [ { "label": "CMake Debug Build", "type": "shell", "command": "mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && make", "args": [], "group": { "kind": "build", "isDefault": true }, "problemMatcher": [], "detail": "Builds the CUDA project in debug mode using CMake" } ] }
This task will automatically create a build directory (if it doesn't exist), run CMake with debug configuration, and compile your project.
Step 4: Test the Debug Setup
- Press
Ctrl+Shift+Bfirst to run the build task manually and ensure there are no compilation errors. - Set breakpoints in your
main.cufile (both host and device code should work with cuda-gdb). - Press
F5to start debugging—you should now hit your breakpoints without the garbled error message you saw earlier.
内容的提问来源于stack exchange,提问作者hh zhao

