答案是配置tasks.json和c_cpp_properties.json文件。首先确认编译器版本支持C++17/20,然后在tasks.json的args中添加-std=c++17或-std=c++20以指定编译标准,在c_cpp_properties.json中设置cppStandard为"c++17"或"c++20"并正确配置compilerPath,确保IntelliSense与编译器一致,最后通过示例代码验证配置是否成功。

在VS Code里配置C++17或C++20标准进行编译,说白了,核心就是两件事:第一,告诉你的编译器(比如GCC或Clang)要用哪个C++标准来编译代码;第二,告诉VS Code的C/C++扩展(主要是IntelliSense)你的项目是基于哪个标准写的,这样它才能正确地提供代码补全、错误检查等功能。VS Code本身并不直接编译,它只是一个强大的编辑器和IDE前端,真正执行编译任务的还是你系统里安装的那个编译器。
要让VS Code环境下的C++项目支持C++17或C++20标准,你需要调整两个关键文件:
tasks.json
c_cpp_properties.json
1. 确认你的编译器版本
这是所有配置的基础。C++17标准通常需要GCC 7或更高版本,或者Clang 5或更高版本。而C++20则需要GCC 9或更高版本,或者Clang 10或更高版本。如果你使用的是旧版编译器,无论怎么配置,都无法编译C++17/20的代码。 你可以在终端里运行
g++ --version
clang++ --version
2. 配置 tasks.json
立即学习“C++免费学习笔记(深入)”;
tasks.json
创建或打开 tasks.json
Ctrl+Shift+P
Cmd+Shift+P
修改编译选项: 找到你的编译任务(通常是
label
args
-std=c++17
-std=c++20
一个典型的
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build active file with C++20",
"type": "shell",
"command": "g++", // 或者 "clang++"
"args": [
"-g", // 启用调试信息
"${file}", // 当前活动文件
"-o", // 输出文件
"${fileDirname}/${fileBasenameNoExtension}", // 输出路径和文件名
"-std=c++20", // 关键在这里,指定C++20标准
"-Wall", // 开启所有警告
"-Wextra" // 开启额外警告
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]
}注意: 如果你在Windows上,路径分隔符可能需要是双反斜杠
\
"${fileDirname}\\${fileBasenameNoExtension}.exe"3. 配置 c_cpp_properties.json
c_cpp_properties.json
创建或打开 c_cpp_properties.json
.cpp
Ctrl+Shift+P
Win32
Linux
Mac
修改 cppStandard
cppStandard
"c++17"
"c++20"
一个典型的
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32", // 或者 "Linux", "Mac"
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.19041.0", // 仅Windows可能需要
"compilerPath": "C:/msys64/mingw64/bin/g++.exe", // 你的编译器路径,很重要
"cStandard": "c17", // 如果你也有C代码,可以配置
"cppStandard": "c++20", // 关键在这里,指定C++20标准
"intelliSenseMode": "gcc-x64" // 或者 "clang-x64", "msvc-x64"
}
],
"version": 4
}小提示:
compilerPath
4. 编写并测试一个C++20示例
完成以上配置后,你可以尝试编写一个使用C++20特性的代码来验证。
// main.cpp
#include <iostream>
#include <vector>
#include <ranges> // C++20 Ranges库
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// 使用C++20 Ranges和Lambda表达式
// 过滤出偶数,然后将它们乘以2
for (int n : numbers | std::views::filter([](int x){ return x % 2 == 0; })
| std::views::transform([](int x){ return x * 2; })) {
std::cout << n << " ";
}
std::cout << std::endl;
// C++20 Designated Initializers (适用于聚合类型)
struct Point { int x; int y; };
Point p = {.x = 100, .y = 200}; // C++20特性
std::cout << "Point coordinates: (" << p.x << ", " << p.y << ")" << std::endl;
return 0;
}保存这个文件,然后按
Ctrl+Shift+B
Cmd+Shift+B
这绝对是初学者,甚至是一些有经验的开发者都会遇到的头疼问题。在我看来,通常有几个常见的原因导致这种情况:
<ranges>
g++ --version
clang++ --version
sudo apt update && sudo apt install g++
tasks.json
tasks.json
-std=c++17
-std=c++20
tasks.json
args
c_cpp_properties.json
c_cpp_properties.json
cppStandard
compilerPath
Ctrl+Shift+P
g++
clang++
where g++
which g++
<ranges>
compilerPath
IntelliSense是VS Code里C/C++开发体验的核心,它负责代码补全、错误提示、定义跳转等。如果它不能正确识别C++20特性,那开发体验会大打折扣。要让IntelliSense“懂”C++20,主要就是调整
c_cpp_properties.json
cppStandard
"c++20"
"c++17"
"cppStandard": "c++20", // 确保是这个
compilerPath
compilerPath
"compilerPath": "C:/msys64/mingw64/bin/g++.exe", // 指向你支持C++20的编译器
如果你用的是Linux或macOS,路径可能是
/usr/bin/g++
/usr/local/bin/clang++
intelliSenseMode
"gcc-x64"
"clang-x64"
"msvc-x64"
"intelliSenseMode": "gcc-x64", // 或者 "clang-x
以上就是VS Code如何配置C++17或C++20标准进行编译的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号