VSCode远程Linux断点调试C++

VSCode远程Linux断点调试C++,第1张

由于手上有一台强大的Linux工作站,尽管在Windows下使用Visul Studio写C++十分舒服,但是考虑到以后很多工作都是在Linux下完成,所以需要配置C++的远程调试环境。
使用Remote SSH插件连接远程工作站,在项目目录下的.vscode里配置task.jsonlaunch.json如下:

task.json

看起来很长,实际上在Linux下面只写labelg++ build active file的配置就可以,其他是为了方便兼容Windows的cl编译任务

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: cl.exe 生成活动文件",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/nologo",
                "/Fe:",
                "${fileDirname}\Bin\${fileBasenameNoExtension}.exe",
                "${file}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        },
        //这个任务是为了在Windows下清除cl编译器生成的*.obj、*.ilk等文件
        //remove.bat可以参考:https://blog.csdn.net/qq_39091609/article/details/124253746
        {
            "label": "clean win32",
            "type": "shell",
            "command": "./.vscode/remove.bat",
            "options": {
                "cwd": "${fileDirname}"
            },
            "dependsOn": ["C/C++: cl.exe 生成活动文件"]
        },
        {
            "type": "shell",
            "label": "g++ build active file",
            "command": "g++",
            //亲测这几个参数很重要
            "args": [
                "-g", 
                "-O0",
                "${file}", 
                "-D_GLIBCXX_DEBUG",
                "-o", 
                "${fileDirname}/Bin/${fileBasenameNoExtension}"
            ],
            "options": {
              "cwd": "${fileDirname}"
            },
            "problemMatcher": ["$gcc"],
            "group": {
              "kind": "build",
              "isDefault": true
            }
        }

    ],
    "version": "2.0.0"
}
launch.json

同样的配置了两个调试启动器,可以在调试界面选择使用哪个启动器,Linux下用G++,Windows用cl.exe

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "cl.exe - 生成和调试活动文件",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${fileDirname}\Bin\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "console": "externalTerminal",
            "preLaunchTask": "clean win32"
        },
        {
            "name": "g++ Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/Bin/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
              {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
              }
            ],
            "preLaunchTask": "g++ build active file",
            "miDebuggerPath": "/usr/bin/gdb"
            //禁止[1] + Done "/usr/bin/gdb" --interpreter显示在终端
            "miDebuggerArgs": "-q -ex quit; wait() { fg >/dev/null; }; /usr/bin/gdb -q --interpreter=mi"
          }
    ]
}

配置完成后按F5即可断点调试

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/707234.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-24
下一篇 2022-04-24

发表评论

登录后才能评论

评论列表(0条)

保存