2021-02-06

VS Code + MinGW搭建C/C++编译环境

安装VS Code,将MinGW免安装版解压后配置环境变量,然后检测GCC编译是否生效gcc -v。

C:\SoftWare\mingw64\bin

 

GCC编译器生效后,在VS Code中创建C/C++工程,VS Code需要配置launch.json和tasks.json

分别如下:

{
    // 使用 IntelliSense 了解相关属性。 `
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,       // 表示使用外部终端还是VS Code的终端界面
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\SoftWare\\mingw64\\bin\\gdb.exe",
            "preLaunchTask": "g++", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc  
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++", //这里注意一下,见下文
            "command": "C:\\SoftWare\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:\\SoftWare\\mingw64\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ]
        }
    ]
}

以上配置完成后,F5运行程序

#include <iostream>
#include <vector>

int main()
{
    std::vector<int> v(10, 5);
    
    for (auto &i : v) {
        std::cout << i << std::endl;
    }
    std::cout << "Hello World!" << std::endl;
    return 0;
}

运行结果如下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值