VS Code 对C/C++的编译(构建)和运行(调试)

引言

本例主要说明了使用了配置文件的方式对C/C++代码进行编译和运行的方法。
如嫌麻烦,现常用扩展“Code Runner",下载并重载后,右上角会出现一个箭头,直接点击即可。

具体步骤

一、创建、编译及运行C/C++工程和代码

1.配置(Configuring)

Ctrl+Shift+P,输入命令C/Cpp: Edit configurations(命令栏应该有提示)在这里插入图片描述
会生成c_cpp_properties.json文件,其中包含了gcc.exe编译器的路径,默认应该没什么问题,如果不对,在mingw路径中找正确路径并修改。

//c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "D:\\WorkSoftware\\MinGW\\bin\\gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

2.构建(Building)

需要生成一个tasks.json文件。
1.打开命令面板(Ctrl+Shift+P)。
2.选择Tasks: Configure Task命令,单击Create Tasks。将看到一个任务运行器模板列表。
在这里插入图片描述
在这里插入图片描述
3.选择Others创建运行外部命令的任务。
现在应该看到一个任务。json文件在工作区.vscode文件夹中。

//模板
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "echo Hello"
        }
    ]
}

4.对tasks.json进行一些修改
可将command更改为用于构建应用程序的命令行表达式(例如g++)。
添加任何必需的arg(例如 -g 构建用于调试)。
还可以将标签更改为更具描述性。
如果像通过Tasks: Run Build Task (Ctrl+Shift+B)来构建程序,可以添加构建group。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build hello world",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g", "vscodetextc1.c"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

在这里插入图片描述

3.调试(Debugging)

如果需要调试,则需要生成一个launch.json
单击补充工具栏中的“调试”图标,导航到“调试”视图。
在这里插入图片描述

在“ 调试”视图中,单击“ 配置”图标。
在这里插入图片描述
从“ 选择环境”下拉列表中选择C++ (GDB/LLDB)(使用GDB或LLDB)或C++ (Windows)(以使用Visual Studio Windows调试程序)。这将使用两种配置创建一个用于编辑的文件: launch.json
在这里插入图片描述
C ++ Launch定义了在开始调试时启动应用程序的属性。
C ++ Attach定义了附加到已经运行的进程的属性。
此处我们选择了第一个C++(GDB/LLDB)

//最初
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "enter program name, for example ${workspaceFolder}/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "/path/to/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

program修改到正在调试的程序的路径;
miDebuggerPath修改指到mingw中的gdb.exe;
如果希望在开始调试时构建应用程序,可以添加一个preLaunchTask属性,其中包含在其中创建的构建任务的名称tasks.json(就是tasks.json中label的值,在上面的示例中为“build hello world”)。

launch.json的备忘

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "D:/WorkSoftware/MinGW/bin/gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build hello world"
        }
    ]
}

点击调试窗“变量”上方绿色箭头或者菜单栏“调试->启动调试”或者 F5。

引用与感谢

https://code.visualstudio.com/docs/languages/cpp#_windows-debugging-with-gdb

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值