Windows配置C/C++环境:MinGW+Vscode

Windows配置C/C++环境

  • 主要分为编译器(编译工具链)编辑器的配置
  • 编译工具链:MinGw-w64
  • 编辑器:VSCode

1. 编译工具

1.1 MinGw-w64安装

通过MSYS2安装

  1. 下载MSYS2:MSYS2

  2. 打开MSYS2执行下面的指令

    bash 复制代码
    pacman -Syu
    pacman -S --needed base-devel mingw-w64-x86_64-toolchain

如果在这个地方报错了,比如你就再试一下,可能是网络不好

bash 复制代码
error: failed retrieving file 'binutils-2.45-1-x86_64.pkg.tar.zst' from mirror.msys2.org : Operation too slow. Less than 1 bytes/sec transferred the last 10 seconds
error: failed retrieving file 'mingw-w64-x86_64-tcl-8.6.16-1-any.pkg.tar.zst' from mirror.msys2.org : Operation too slow. Less than 1 bytes/sec transferred the last 10 seconds
error: failed retrieving file 'mingw-w64-x86_64-expat-2.7.3-1-any.pkg.tar.zst' from mirror.msys2.org : Operation too slow. Less than 1 bytes/sec transferred the last 10 seconds
warning: too many errors from mirror.msys2.org, skipping for the remainder of this transaction
error: failed retrieving file 'binutils-2.45-1-x86_64.pkg.tar.zst.sig' from repo.msys2.org : Operation too slow. Less than 1 bytes/sec transferred the last 10 seconds
error: failed retrieving file 'mingw-w64-x86_64-expat-2.7.3-1-any.pkg.tar.zst.sig' from repo.msys2.org : Operation too slow. Less than 1 bytes/sec transferred the last 10 seconds
error: failed retrieving file 'mingw-w64-x86_64-bzip2-1.0.8-3-any.pkg.tar.zst' from repo.msys2.org : Operation too slow. Less than 1 bytes/sec transferred the last 10 seconds
warning: too many errors from repo.msys2.org, skipping for the remainder of this transaction
error: failed retrieving file 'mingw-w64-x86_64-libtre-0.9.0-1-any.pkg.tar.zst' from repo.msys2.org : Operation too slow. Less than 1 bytes/sec transferred the last 10 seconds
warning: failed to retrieve some files
error: failed to commit transaction (unexpected error)
Errors occurred, no packages were upgraded.

1.2 配置环境变量

将 MinGW 的 bin 目录添加到 PATH

例如:C:\msys64\mingw64\bin

  • 右键"此电脑" → 属性 → 高级系统设置 → 环境变量

1.3 检查

打开你的Powshell,输入gcc -v,显示得到版本就行,g++gdb同理

2. 编辑器

2.1 VScode下载

Download Visual Studio Code - Mac, Linux, Windows

2.2 插件安装

插件商店搜索下面的进行安装就行:

  • C/C++ Extension Pack

2.3 JSON文件编写

具体参考我的另外一篇笔记:

需要有这写.vscode文件,常用快捷键如下

  • 编译:ctrl + shift +B
  • 调试:F5

编译的时候选择你要执行的任务,这个在task.json的lable中有写;

在这我就贴出我常用的JSON模板

  • task.json
json 复制代码
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build main.exe",
            "type": "shell",
            "command": "g++",
            "args": 
            [
                "-std=c++20",
                "-o",
                "${workspaceFolder}/main",
                "${workspaceFolder}/hot100/189_rotate.cpp"
            ],
            "group": "build"
        },
        {
            "type": "cppbuild",
            "label": "build main.out",
            "command": "g++",
            "args":
            [
                "-std=c++20",
                "-g",
                "-o",
                "${workspaceFolder}/main.out",
                "${workspaceFolder}/hot100/189_rotate.cpp"
            ],
            "group": {
                "kind": "build",
                "isDefault": false
            },
            "detail": "Task generated by Debugger."
        }
    ]
  • launch.json
json 复制代码
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/main.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build main.out" //在launch之前运行的任务名
        }
    ]
}
相关推荐
恋爱绝缘体17 小时前
2020重学C++重构你的C++知识体系
java·开发语言·c++·算法·junit
Z1Jxxx8 小时前
加密算法加密算法
开发语言·c++·算法
张世争8 小时前
windows clion MingW cmake 编译运行 FreeRTOS
windows·freertos·mingw·cmake·clion
乌萨奇也要立志学C++8 小时前
【洛谷】递归初阶 三道经典递归算法题(汉诺塔 / 占卜 DIY/FBI 树)详解
数据结构·c++·算法
AI 智能服务8 小时前
第6课__本地工具调用(文件操作)
服务器·人工智能·windows·php
️停云️9 小时前
【滑动窗口与双指针】不定长滑动窗口
c++·算法·leetcode·剪枝·哈希
charlie1145141919 小时前
嵌入式现代C++教程: 构造函数优化:初始化列表 vs 成员赋值
开发语言·c++·笔记·学习·嵌入式·现代c++
IDC02_FEIYA9 小时前
SQL Server 2025数据库安装图文教程(附SQL Server2025数据库下载安装包)
数据库·windows
IT=>小脑虎9 小时前
C++零基础衔接进阶知识点【详解版】
开发语言·c++·学习
Felven9 小时前
A. Helmets in Night Light
c语言