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++"
		}
	]
}
相关推荐
纵有疾風起4 小时前
C++—string(1):string类的学习与使用
开发语言·c++·经验分享·学习·开源·1024程序员节
javaGHui7 小时前
macOS 上获取调试版
macos
郭源潮18 小时前
《Muduo网络库:实现TcpServer类终章》
服务器·网络·c++·网络库
MMjeaty11 小时前
查找及其算法
c++·算法
2501_9159184111 小时前
iOS描述文件功能解析
android·macos·ios·小程序·uni-app·cocoa·iphone
yong158585534312 小时前
1. Linux C++ muduo 库学习——库的编译安装
linux·c++·学习
mit6.82413 小时前
回溯剪枝trick
c++
渡我白衣13 小时前
C++世界的混沌边界:undefined_behavior
java·开发语言·c++·人工智能·深度学习·语言模型
却道天凉_好个秋14 小时前
c++ 协程
c++
无敌最俊朗@15 小时前
视频时间基 (time_base) 详解:时间的“刻度单位”
c++