vscode配置C++编译和调试环境

1配置文件

一般vscode配置C++有三个文件,它们分别是:

1.1.c_cpp_properties.json

设置编译环境参数。通过Ctrl+Shift+P,输入C++,在下拉菜单中选择“C/C++ Edit configuration”,系统自动会在.vscode目录下创建该文件,供我们设置编译环境。可根据自己需求改动如下配置,默认配置如下:

{
    "configurations": [
        {
            "name": "Win32", // 环境名称
            "includePath": [ // 头文件包含路径,当前指定的是工作目录,有需要可添加,加入相应的文件路径即可
                "${workspaceFolder}/**"
            ],
            "defines": [     // 预处理定义
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\mingw64\\bin\\gcc.exe", // 编译器路径
            "cStandard": "gnu17",  // 设置C标准库
            "cppStandard": "gnu++14", // 设置C++标准库
            "intelliSenseMode": "gcc-x64" // 设置补全模式
        }
    ],
    "version": 4
}

 

1.2.tasks.json

设置编译参数,通过Ctrl+Shift+p,输入task,在下拉菜单中选择Tasks: Configure Default Build Task -> Create task.json file from templates -> Others,系统自动在.vscode下创建task.json文件,供我们设置具体的编译规则。根据实际请求修改如下:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build C++ train", // 当前编译任务名字
            "type": "shell",
            "command": "g++",  // 编译时执行的程序
            "args": ["-g", "${workspaceFolder}/*.cpp"  , "-o", "train"], // 传递给command的参数,也就是编译参数
            "group": {
                "kind": "build",
                "isDefault": true // true,用户可以通过Ctrl+Shift+B直接运行编译任务
            }
        }
    ]
}

 

其中:${workspaceFolder}表示当前工作文件夹,加上/*.cpp表示多所有的.cpp文件进行编译。

通过编译之后,就会自动链接生成可执行文件。

3.launch.json

设置调试参数。通过Ctrl+Shift+P打卡命令行,输入“lauch”选择“Debug: Open launch.json” -> "C++(GDB/LLDB)",即可打开调试配置文件launch.json。配置后之后,按F5,进入调试模式,配置如下:

{
    // 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}/a.exe", // 设置exe路径
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/path/to/gdb", // 设置当前系统的gdb路径,windows下是gdb.exe
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

  

进行Debug的前提,需要在可执行程序时加入了编译选项-g。这样gdb才可以调试。

posted @ 2020-11-17 11:33  绍荣  阅读(2553)  评论(0编辑  收藏  举报