解决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": []
        }
    ]
}

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

相关推荐
2501_916007479 小时前
Windows OpenSSL 生成 iOS 证书,制作 p12 与描述文件,KXBuild 签名实操
windows·vscode·ios·objective-c·个人开发·swift·敏捷流程
椒颜皮皮虾྅11 小时前
模型部署不再卡在环境上:DeploySharp 四大推理引擎与 Windows/Linux 离线交付实战
linux·运维·开发语言·人工智能·windows·计算机视觉·cuda
我要见SA姐111 小时前
Chrome 内存泄漏诊断指南:从现象定位到根因排查
ide·vscode·python·自动化·编辑器
椒颜皮皮虾྅11 小时前
让 C# 读懂整张图:DeploySharp 驱动 PaddleOCR v4/v5/v6,打通 OCR 全流程
开发语言·人工智能·深度学习·学习·c#·ocr·cuda
00后程序员张12 小时前
Windows 开发 iOS App 入门,用 SwiftUI 写第一个 iOS App 装到 iPhone
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
瓶 盖12 小时前
你的 IDE 插件改了 authorized_keys:Claude Code 扩展投毒链路拆解
ide·ssh·vs code·安全分析·供应链安全·claude code
香芋芋圆1 天前
iD-editor 详解|OSM官方在线编辑器,基于D3.js的轻量地图编辑器
开发语言·javascript·编辑器
qinglongcn1 天前
Agent 可能出什么事——五类异常与应对
人工智能·编辑器·ai编程
QianhangQianping1 天前
撤销重做不是「存快照」:一个可视化编辑器的Operation 设计
java·前端·数据库·typescript·编辑器·前端架构·撤销重做
Eiceblue1 天前
用 Python 给 Word 文档加水印:文字、图片与批量处理
开发语言·vscode·python·word