如何更新GCC以支持C++20?VS Code中g++编译环境升级疑问
升级MinGW g以支持C20并适配VS Code
一、运行mingw-get update的位置
- 若MinGW的
bin目录(比如C:\MinGW\bin)已添加到系统环境变量PATH,直接打开Windows的「命令提示符」(CMD)或「PowerShell」,输入命令即可。 - 未添加PATH的话,打开文件资源管理器找到MinGW的
bin文件夹,在地址栏输入cmd回车,在弹出的命令窗口里直接执行mingw-get update。
二、升级g到支持C20的版本
先同步包列表:
mingw-get update安装/升级支持C20的g版本:
mingw-get install mingw32-gcc-g++若你用的是64位MinGW-W64,命令改为
mingw-get install mingw-w64-x86_64-gcc-g++,根据实际安装路径调整。验证版本:
运行以下命令,确认g版本在10.0及以上(该版本起对C20核心特性支持完整):g++ --version
三、配置VS Code启用C++20编译
- 创建或修改
.vscode文件夹下的tasks.json,添加C++20编译参数:
示例配置片段:{ "version": "2.0.0", "tasks": [ { "label": "g++ build active file", "type": "shell", "command": "g++", "args": [ "-std=c++20", "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": ["$gcc"], "group": { "kind": "build", "isDefault": true } } ] } - 若使用C/C++扩展,在
c_cpp_properties.json里设置标准版本:{ "configurations": [ { "name": "Win32", "includePath": ["${workspaceFolder}/**"], "defines": ["_DEBUG", "UNICODE", "_UNICODE"], "compilerPath": "C:/MinGW/bin/g++.exe", "cStandard": "c17", "cppStandard": "c++20", "intelliSenseMode": "windows-gcc-x86" } ], "version": 4 }
四、测试验证
创建test.cpp文件,写入以下代码:
#include <iostream> #include <vector> #include <ranges> #include <span> int main() { std::vector<int> nums = {1,2,3,4,5}; std::span<int> s(nums); auto even = nums | std::views::filter([](int n){return n%2 ==0;}); for(int n : even){ std::cout << n << " "; } return 0; }
按Ctrl+Shift+B编译运行,能正常输出2 4即配置成功。
内容的提问来源于stack exchange,提问作者Clockworkedd
相关产品推荐
相关产品推荐

