解决vscode在win下使用cuda无法跳转库函数的问题

安装相关插件

在项目下的.vscode文件夹中正确配置版本和相关文件的路径,有些路径需要根据你电脑中的实际路径进行配置,如果某个路径有标黄的波浪线,说明vscode没有找到这个路径

c_cpp_properties.json

复制代码
{
    "configurations": [
        {
            "name": "Win64",
            "includePath": [
                "${workspaceFolder}/**",
                // 明确CUDA include路径(**确保递归解析子目录)
                "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.4/include/**",
                // 替换为你实际的MSVC版本路径(手动找:VS2022安装目录→VC→Tools→MSVC→具体版本号)
                "C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.43.34808/include",
                // 替换为你实际的Windows SDK版本(手动找:Windows Kits/10/Include/具体版本号)
                "C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/um",
                "C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/shared",
                "C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/winrt" // 补充winrt路径(可选)
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE",
                "__CUDACC__",
                "_WIN64",
                "CUDA_VERSION=12040" // 显式定义CUDA版本(辅助IntelliSense解析)
            ],
            // 适配CUDA的IntelliSense模式(优先选cuda-msvc-x64,无则用windows-msvc-x64)
            "intelliSenseMode": "windows-msvc-x64",
            "compilerPath": "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.4/bin/nvcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "browse": {
                "path": [
                    "${workspaceFolder}/**",
                    "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.4/**"
                ],
                "limitSymbolsToIncludedHeaders": false, // 允许解析所有CUDA头文件符号
                "databaseFilename": ""
            }
        }
    ],
    "version": 4
}

launch.json

复制代码
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "CUDA Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/main.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "D:/Program Files/mingw64/bin/gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build",
            "postDebugTask": "clean"
        }
    ]
}

settings.json(这个文件非常重要,如果没有,基本没法跳转)

复制代码
{
    "C_Cpp.default.includePath": [
        "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.4/include",
        "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.4/include/cublas",
        "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.4/include/cudnn" // 如果用cudnn
    ],
    "C_Cpp.default.defines": [
        "__CUDACC__",
        "CUDA_VERSION=12040"
    ],
    "files.associations": {
        "*.cu": "cpp",
        "*.cuh": "cpp",
        "cuda_runtime.h": "cpp",
        "cublas*.h": "cpp"
    }
}

tasks.json,这个文件的配置根据自己的实际情况决定,不需要跟我的一样

复制代码
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v12.4\\bin\\nvcc.exe",
            "args": [
                "${workspaceFolder}/main.cu",
                "-o",
                "${workspaceFolder}/main.exe",
                "-arch=sm_60",

                "-std=c++17"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": ["$nvcc"],
            "detail": "使用nvcc编译CUDA程序"
        },
        {
            "label": "run CUDA",
            "type": "shell",
            "command": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "dependsOn": "build",
            "group": {
                "kind": "test",
                "isDefault": true
            },
            "problemMatcher": []
        },
        {
            "label": "clean",
            "type": "shell",
            "command": "del",
            "args": [
                "${workspaceFolder}\\main.exe",  
                "/f",
                "/q"
            ],
            "problemMatcher": []
        }
    ]
}

配置好之后就可以正确跳转了

相关推荐
jun_bai1 小时前
VSCode使用
ide·vscode·编辑器
猫头虎3 小时前
OpenClaw-VSCode:在 VS Code 里玩转 OpenClaw,远程管理+SSH 双剑合璧
ide·vscode·开源·ssh·github·aigc·ai编程
手揽回忆怎么睡5 小时前
opencode和TRAE使用Superpowers 和ui-ux-pro-max skillls
ide·ui·ai·ux
CaracalTiger5 小时前
OpenClaw-VSCode:在 VS Code 中通过 WebSocket 远程管理 OpenClaw 网关的完整方案
运维·ide·人工智能·vscode·websocket·开源·编辑器
CS创新实验室6 小时前
Pandas 3 的新功能
android·ide·pandas
-嘟囔着拯救世界-6 小时前
【2026 最新版】OpenAI 祭出王炸 GPT-5.3-Codex!Win11 + VSCode 部署保姆级教程
vscode·gpt·chatgpt·node.js·node·codex·gpt5
Web极客码7 小时前
WordPress从经典编辑器升级到古腾堡编辑器
运维·编辑器·wordpress
江湖有缘7 小时前
Docker部署music-tag-web音乐标签编辑器
前端·docker·编辑器
ouliten16 小时前
cuda编程笔记(36)-- 应用Tensor Core加速矩阵乘法
笔记·cuda
先跑起来再说1 天前
Git 入门到实战:一篇搞懂安装、命令、远程仓库与 IDEA 集成
ide·git·后端·elasticsearch·golang·intellij-idea