欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

VS Code调试C++:Ubuntu

程序员文章站 2022-07-09 17:04:03
...

1 环境

  • Unbuntu18.04
  • VS Code1.48.2

2 添加调试配置文件

2.1 GDB调试

Unbutu使用GDB/LLBD模式调试.

VS Code调试C++:Ubuntu

图1 GDB/LLDB调试

2.2 添加配置文件

使用默认配置,生成launch.json文件.
VS Code调试C++:Ubuntu

图2 添加默认配置文件

2.3 launch.json文件

VS Code调试C++:Ubuntu

图3 launch.json文件
  • launch.json
    添加:preLaunchTask: build
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "preLaunchTask": "build",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}
  • tasks.json
    在.vscode文件夹中新建tasks.json,内容如下:
    使用g++编译文件,参数为args.
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": ["-g", "${file}", "-std=c++11", "-o", "${fileBasenameNoExtension}.out"]
        }
    ]
}

3 调试

3.1 直接运行

VS Code调试C++:Ubuntu

图4 运行程序

3.2 分步调试:断点调试

VS Code调试C++:Ubuntu

图5 分步调试程序

[参考文献]
[1]https://blog.csdn.net/hitzijiyingcai/article/details/90751478
[2]https://zhuanlan.zhihu.com/p/110027894?from_voters_page=true