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文件,供我们设置具体的编译规则。根据实际请求修改如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
    // 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,进入调试模式,配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{
    // 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 @   绍荣  阅读(2890)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示