• 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

    {
      "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"
      }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

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

    {
    	// 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++"
    		}
    	]
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
  • 相关阅读:
    开发调试工具:USB转IIC/I2C/SPI/UART适配器模块可编程开发板
    第三章 ArcGIS Pro创建 python 脚本工具(四)
    C++ day4
    面试了一个34岁的java老码农年薪50w的面试题基本都能答得上
    Springboot + Elasticjob-lite 3.x + Elasticjob-lite-UI可视化
    响应式数据
    韩语学习|韩语零基础|柯桥韩语学校,每日一词
    聊聊SEO与生意的关系,顺带说说百度快照功能下线原因及影响有哪些?
    【汇编】处理字符问题
    Vue.js源码解析:Vue.js 常用工具函数 util.js
  • 原文地址:https://blog.csdn.net/qq_37604640/article/details/134257891