You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 20环境下如何在VS Code中调试CMake构建的CUDA程序?

Fixing CUDA Debugging with VS Code + CMake on Ubuntu 20.04

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 compiled main executable (assuming you use out-of-source build in a build folder; if you build in the root, use ${workspaceFolder}/main instead).
  • 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

  1. Press Ctrl+Shift+B first to run the build task manually and ensure there are no compilation errors.
  2. Set breakpoints in your main.cu file (both host and device code should work with cuda-gdb).
  3. Press F5 to start debugging—you should now hit your breakpoints without the garbled error message you saw earlier.

内容的提问来源于stack exchange,提问作者hh zhao

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 23:12:36