VSCode编译环境搭建教程

VSCode C/C++ 编译调试
本文介绍如何在VSCode中搭建C/C++的编译和调试环境,包括下载MinGW编译器、配置c_cpp_properties.json指定库文件夹、通过tasks.json指定编译器及生成文件、使用launch.json指定调试器及调试目标。

VSCode编译环境搭建

1.下载编译器MinGW

(MinGW:Minimalist GNU for Windows)

(1)官网下载(可以翻墙的条件下)

此部分可以参考优快云博客:https://blog.youkuaiyun.com/bat67/article/details/76095813

==(注:不翻墙下载的文件会不完整)==

(2)下载pixhawk集成工具包

下载地址:http://firmware.ardupilot.org/Tools/PX4-tools/pixhawk_toolchain_installer_latest.exe

2.VSCode调试环境配置

参考官方文档:https://code.visualstudio.com/docs/languages/cpp

VSCode下配置C/C++调试参数的文件有三个:

(1) 找不到包含的库时(如果安装了ms-vscode.cpptools第三方插件,会有波浪线)按下Ctrl+Shift+P会生成一个c_cpp_properties.json文件,==c_cpp_properties.json文件指定库文件夹==。示例代码如下:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
               "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "msvc-x64"
        }
    ],
    "version": 4
}

(2)Ctrl+Shift+P打开命令板,选择Tasks: Configure Tasks,点击 tasks.json file from templates,继续点击Others,这时会生成tasks.json文件。==tasks.json文件指定编译器和生成文件==。示例代码如下:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build eigen",
            "type": "shell",
            "command": "g++",
            "args": ["-g","${file}","-o","${fileBasenameNoExtension}.exe"],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

(3)为了能进行调试,需要launch.json文件。按F5或这点击调试会生成该文件。==launch.json指定调试器和要调试的exe文件==。示例代码如下:

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

至此,调试参数配置完成。

### 配置 VSCode 使用 MinGW-W64 编译 C/C++ 程序 #### 安装 Visual Studio Code 和 MinGW-w64 Visual Studio Code 是一款轻量级但功能强大的源代码编辑器,支持多种编程语言。然而,VSCode 本身并不具备编译能力,因此需要额外安装编译工具链。 对于 Windows 用户来说,推荐使用 MinGW-w64 来作为 GCC 工具链[^1]。MinGW-w64 提供了一套完整的 GNU 开发工具集,包括 gcc、g++ 等用于编译 C/C++ 的命令行工具。 下载完成后需将 MinGW 解压缩到指定文件夹,并将其 bin 目录路径添加至系统的环境变量 PATH 中以便全局调用这些可执行文件[^2]。 #### 安装扩展插件 为了更好地支持 C/C++ 项目,在 VSCode 内部应安装 Microsoft 发布的官方 C/C++ 扩展包。此扩展提供了 IntelliSense 语法高亮显示以及调试等功能特性。 #### 创建工作区与编写第一个程序 创建一个新的文件夹作为工作空间,打开该目录下的任意位置新建名为 `main.c` 或者 `main.cpp` 文件,输入简单的 Hello World 示例: ```c #include <stdio.h> int main() { printf("Hello, world!\n"); return 0; } ``` #### 设置任务构建脚本 接下来定义一个 JSON 格式的任务配置文件 `.vscode/tasks.json` ,它告诉 VSCode 如何调用外部编译器完成项目的编译过程: ```json { "version": "2.0.0", "tasks": [ { "label": "build hello", "type": "shell", "command": "gcc", "args": [ "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"], "detail": "Generated task from snippet." } ] } ``` 上述配置会自动检测当前活动文档的位置并将生成的目标文件保存在同一文件夹下。 #### 启动调试模式 最后一步是在 launch.json 中设定启动参数以允许单步跟踪执行流程或查看内存状态变化等操作。这里给出一段适用于大多数情况的基础模板: ```json { "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "C:\\path\\to\\mingw64\\bin\\gdb.exe", // 更改为实际GDB路径 "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "build hello", "internalConsoleOptions": "openOnSessionStart" } ] } ``` 请注意修改 `"miDebuggerPath"` 字段指向本地已安装 GDB 可执行文件的确切地址。
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值