Mac VsCode g++编译报错:不支持C++11语法解决

编译运行时报错:

Running cd "/Users/yiran/Documents/vs_projects/c++/" && g++ 1116.cpp -o 1116 && "/Users/yiran/Documents/vs_projects/c++/"1116

1116.cpp:28:22: warning: range-based for loop is a C++11 extension -Wc++11-extensions

报错截图:

解决方案:

在.vscode/settings.json中配置如下,目的是在编译命令中添加编译选项-std=c++11

json 复制代码
{
  "C_Cpp.errorSquiggles": "enabled",
  "files.associations": {
    "vector": "cpp"
  },
  // 添加如下配置,对应文件路径替换成对应自己的路径
  "code-runner.executorMap": {
    "cpp": "cd '/Users/yiran/Documents/vs_projects/c++/' && g++ -std=c++11 $fullFileName -o $fileNameWithoutExt && ./$fileNameWithoutExt"
  }

}

如果不生效,也可以在.vscode/tasks.json中添加如下配置,在编译命令中添加编译选项-std=c++11

json 复制代码
{
	// See https://go.microsoft.com/fwlink/?LinkId=733558
	// for the documentation about the tasks.json format
	"version": "2.0.0",
	"tasks": [
		{
			"type": "shell",
			"label": "clang++ build active file",
			"command": "/usr/bin/clang++",
			"args": [
				"-std=c++17",
				"-stdlib=libc++",
				"-g",
				"${file}",
				"-o",
				"${fileDirname}/${fileBasenameNoExtension}"
			],
			"options": {
				"cwd": "${workspaceFolder}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": "build"
		},
		{
			"type": "cppbuild",
			"label": "C/C++: clang 生成活动文件",
			"command": "/usr/bin/clang",
			"args": [
				"-std=c++17",
				"-fdiagnostics-color=always",
				"-g",
				"${file}",
				"-o",
				"${fileDirname}/${fileBasenameNoExtension}"
			],
			"options": {
				"cwd": "${fileDirname}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"detail": "编译器: /usr/bin/clang"
		},
		{	
			"type": "cppbuild",
			"label": "C/C++: g++ 生成活动文件",
			"command": "/usr/bin/g++",
			"args": [
				"-std=c++17",
				"-fdiagnostics-color=always",
				"-g",
				"${file}",
				"-o",
				"${fileDirname}/${fileBasenameNoExtension}"
			],
			"options": {
				"cwd": "${fileDirname}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": "build",
			"detail": "编译器: /usr/bin/g++"
		}
	]
}
相关推荐
钓鱼的肝2 小时前
梳理(1-5)
c++·经验分享·笔记·算法·青少年编程
我要见SA姐12 小时前
用 Claude Code 重构遗留系统:从评估到落地的完整实践指南
数据库·ide·vscode·oracle·编辑器
jimy13 小时前
readelf 查看“派生类”的vtable符号
开发语言·c++
蒸蒸yyyyzwd4 小时前
cpp 选手秋招学习笔记 day37 面经学习
c++·八股
YYYing.4 小时前
【C++进阶系列 (七)】C++ RTTI 深度剖析:从 typeid 到 dynamic_cast 的底层之旅
开发语言·c++·c/c++·rtti
钓鱼的肝5 小时前
csp-j-s总结(2)
c++·经验分享·笔记·算法·青少年编程
重生的黑客5 小时前
Qt 常用控件精讲(1):QWidget 核心属性全解 —— 从 geometry 到 qrc 与 QSS
c++·qt·qwidget·qss·常用控件
Hespethorn6 小时前
取 `X-Forwarded-For` 首段做频控 key,等于把限流开关交给了调用方
c++
米糕闯编程6 小时前
鱼香ros2(七)功能包组织C++节点
c++·ros2·cmakelists
汉克老师6 小时前
GESP2026年9月认证C++四级( 第三部分编程题(1、新汉诺塔))精讲
c++·gesp·小学生·学c++编程