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++"
		}
	]
}
相关推荐
笑口常开xpr7 分钟前
【C++】模板 - - - 泛型编程的魔法模具,一键生成各类代码
开发语言·数据结构·c++·算法
AA陈超29 分钟前
虚幻引擎5 GAS开发俯视角RPG游戏 P05-01.创建游戏玩法标签
c++·游戏·ue5·游戏引擎·虚幻
笑口常开xpr1 小时前
【C++继承】深入浅出C++继承机制
开发语言·数据结构·c++·算法
代码AC不AC1 小时前
【C++】红黑树实现
c++·红黑树·底层结构
马儿能够一直跑2 小时前
基于vscode在WSL中配置PlatformIO开发环境
ide·vscode·编辑器
止观止2 小时前
VS Code 二次开发:跨平台图标定制全攻略
linux·windows·vscode·macos
Dontla2 小时前
VSCode括号高亮插件(vscode插件)bracket pair、活动括号对、括号线(未完全检查)
ide·vscode·编辑器
liu****2 小时前
2.c++面向对象(三)
开发语言·c++
linux kernel2 小时前
第二十四讲:C++中的IO流
开发语言·c++
小瓶盖_tl4 小时前
在Mac上安装CocoaPods问题处理
macos·xcode·cocoapods