在Mac vscode默认配置中,c++的标准为c++98
输出c++版本
if (__cplusplus == 201703L) std::cout << "C++17\n";
else if (__cplusplus == 201402L) std::cout << "C++14\n";
else if (__cplusplus == 201103L) std::cout << "C++11\n";
else if (__cplusplus == 199711L) std::cout << "C++98\n";
else std::cout << "pre-standard C++\n";
(1) 配置task.json
Ctrl+Shift+P: 输入task,打开task.json
改成以下内容
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "build",
"command": "/usr/bin/clang++",
"args": [
"-std=c++11",
"-stdlib=libc++",
"-fdiagnostics-color=always",
"${file}",
"-o",
"${fileDirname}/../build/${fileBasenameNoExtension}",
"-g",
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault":true
},
"detail": "编译器: /usr/bin/clang++"
}
]
}
(2) 配置launch.json
或者在这里打开
改成以下内容
{ // 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。 // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Debug", "type": "gdb", "request": "launch", "target": "./bin/executable", "cwd": "${workspaceRoot}", "valuesFormatting": "parseText" } ] }
(3) 配置.vscode/c_cpp_properties.json
打开一个包含.cpp文件的文件夹(没有就自己创建)
“command+shift+p”打开命令行工具窗口,输入或者选择“Edit Configurations”命令。
此时会在当前工作空间目录生成.vscode配置目录,同时在配置目录会生成一个c_cpp_properties.json文件。
改成以下内容
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"],
"compilerPath": "/usr/bin/clang++",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
(4) 配置settings.json
打开settings.json
settings.json中。14行中加了 -std=c++11