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

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

相关推荐
sphw4 小时前
nixnb: Jupyter Notebook 优雅分享
ide·人工智能·jupyter
NoteStream12 小时前
【c语言基础】C语言常见概念
c语言·开发语言·编辑器
布鲁飞丝17 小时前
从零实现富文本编辑器#-浏览器选区与编辑器选区模型同步
java·前端·编辑器
计算机内卷的N天17 小时前
CMake与Visual Studio的使用
c++·ide·visual studio
咱入行浅18 小时前
一款实用的 Visual Studio 发布部署插件,助力提高部署效率!
ide·visual studio
谢斯18 小时前
[vscode] 使用unity打开vscode的取消.csproj的显示
ide·vscode·unity
2501_916007471 天前
SwiftUI 声明式语法与 Xcode 预览功能详解
ide·vscode·ios·swiftui·个人开发·xcode·敏捷流程
呐抹倾1 天前
【译】Visual Studio 停用:针对旧版本 Visual Studio 的支持提醒
ide·visual studio
(initial)2 天前
B-09. 数据布局(AoS/SoA/Transpose):一次布局调整带来的事务变化
cuda
爱就是恒久忍耐2 天前
使用VSCode开发STM32 (通用版本)
ide·vscode·stm32