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++"
		}
	]
}
相关推荐
乄夜19 分钟前
嵌入式面试高频(5)!!!C++语言(嵌入式八股文,嵌入式面经)
c语言·c++·单片机·嵌入式硬件·物联网·面试·职场和发展
YYDS31434 分钟前
C++动态规划-01背包
开发语言·c++·动态规划
wydaicls1 小时前
十一.C++ 类 -- 面向对象思想
开发语言·c++
姜君竹2 小时前
QT的工程文件.pro文件
开发语言·c++·qt·系统架构
思捻如枫2 小时前
C++数据结构和算法代码模板总结——算法部分
数据结构·c++
weixin_478689763 小时前
C++ 对 C 的兼容性
java·c语言·c++
k要开心3 小时前
C++概念以及基础框架语法
开发语言·c++
weixin_307779133 小时前
Linux下GCC和C++实现统计Clickhouse数据仓库指定表中各字段的空值、空字符串或零值比例
linux·运维·c++·数据仓库·clickhouse
且白3 小时前
vsCode使用本地低版本node启动配置文件
前端·vue.js·vscode·编辑器
维克喇叭3 小时前
vscode 离线安装第三方库跳转库
ide·vscode·编辑器