vscode cpp debug/run

ref to

Visual Studio Code 配置C/C++编译环境流程及问题解决(Win10环境)_一只特立独行的程序猿的博客-CSDN博客_g++ -v -e -x c++ -

MingW

mingw-w64-install.exe

Vscode

插件

 ctr+shift+p

tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "D:\\Program Files\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-fexec-charset=GBK",
                "-std=c++17",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

### 如何在 VSCode 中设置 C/C++ 开发环境 #### 安装 Visual Studio Code 和必要组件 为了开始配置 C/C++ 的开发环境,需先下载并安装最新版本的 Visual Studio Code (VSCode)[^1]。 #### 安装 C/C++ 扩展包 通过访问 VSCode 的扩展市场,可以找到由 Microsoft 提供的官方 C/C++ 扩展插件。该插件提供了 IntelliSense 支持、调试支持以及其他特性来增强 C/C++ 编程体验。 #### 创建工作区文件夹结构 创建一个新的项目目录用于存放源代码和其他资源文件。这有助于保持项目的整洁有序,并使得后续配置更加简单明了。 #### 配置 `c_cpp_properties.json` 文件 此 JSON 文件定义了一些编译器路径以及预处理器宏等重要参数。下面是一个典型的例子: ```json { "configurations": [ { "name": "Win32", "includePath": ["${workspaceFolder}/**"], "defines": ["_DEBUG", "UNICODE", "_UNICODE"], "windowsSdkVersion": "8.1", "compilerPath": "D:\\MinGW\\bin\\gcc.exe", "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "${default}" } ], "version": 4 } ``` 上述配置指定了 MinGW 工具链的位置 (`compilerPath`) 及其标准库版本 (`cStandard`, `cppStandard`). 同时也设置了 Windows SDK 版本和一些常用的预处理指令(`defines`), 这些都是针对特定平台的需求所作调整[^2]. #### 设置任务构建命令 为了让 VSCode 能够调用外部工具完成程序编译过程,在 `.vscode/tasks.json` 中添加如下内容: ```json { "tasks": [ { "label": "build hello world", "type": "shell", "command": "g++", "args": [ "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ] } ] } ``` 这段脚本告诉编辑器当执行 “Run Build Task…” 命令时应该怎样去运行 g++ 来编译当前打开的那个 .cpp 或者.c 文件, 并生成相应的可执行文件. #### 使用 launch.json 实现断点调试 最后一步就是让开发者能够在 IDE 内部直接启动应用程序并且能够方便地进行单步跟踪等功能。为此可以在`.vscode/launch.json`里加入以下片段作为基础模板之一: ```json { "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/hello.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 } ], "preLaunchTask": "build hello world", "internalConsoleOptions": "openOnSessionStart" } ] } ``` 以上即是在 VSCode 上搭建起一套完整的 C/C++ 开发流程所需的主要步骤概述.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值