4

vscode 新版配置调试c++(g++)

 2 years ago
source link: https://www.80shihua.com/archives/1597
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

vscode 新版配置调试c++(g++)

作者: dreamfly 分类: c/c++ 发布时间: 2018-12-05 13:03

1.首先在项目目录新建2个配置文件,一个tasks.json文件,一个launch.json文件

2.launch.json文件如下

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [

        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/main.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\MinGW\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build-debug"
        }
    ]
}

其中program:${workspaceFolder}/debug.exe 是要调用的程序,
MIMode:"gdb"是调试的程序,这里采用gdb进行调试,
"miDebuggerPath": "D:\MinGW\bin\gdb.exe",是调试程序的路径
"preLaunchTask":"build-debug"是调试之前的任务名称。

这里build-debug是和下面的tasks.json中的任务名对应的。

意思是我们在调试之前,先调用任务生成main.exe文件

3.tasks.json文件内容

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build-debug",
            "type": "shell",
            "command": "g++",
            "problemMatcher": [],
            "args": [
                "-g",
                "main.cpp",
                "-o",
                "main.exe"
            ]
        }
    ]
}

program:中的调试程序名称需要和args中生成的exe名称一致。

miDebuggerPath:需要配置成绝对路径,否则可能出现路径非法问题。

这里label是任务名称,这是最新版vscode的写法,之前是采用taskName来定义的。

type:"shell"指的是调用方式。

commond:"g++"这里指调用任务的程序,这里是通过g++来编译生成debug.exe文件.

args:[]这里是g++命令的后面提供的参数。

g++的完整命令是:

g++ -g main.cpp -o main.exe

所以我们在args中配置了4个参数作为g++调用使用。

通过以上配置就可以在vscode中调试c++了,但是着些都是在你已经配置好g++,gdb等环境变量的前提下,如何配置g++,gdb等环境变量,请参考
windows配置g++,gdb环境变量

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK