【问题标题】:Compile error on vscode. type specifier is a C++11 extensionvscode编译错误。类型说明符是 C++11 扩展
【发布时间】:2017-05-25 02:32:30
【问题描述】:

如何在 vscode 上使用 C++11 兼容模式编译?

test.cpp

#include <iostream>

using namespace std;

void print()
{
    int v[] = {0,1,2,3,4,5,6,7,8,9};
    for (auto x : v) {
        cout << x << '\n';
    }

    for (auto x : {10,21,32,43,54,65}) {
        cout << x << '\n';
    }
}

int main() {
    print();
}

tasks.json

{
    "version": "0.1.0",
    "command": "g++",
    "isShellCommand": true,
    "args": ["-O2", "-g", "test.cpp"],
    "showOutput": "always"
}

错误

test.cpp:8:10: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
    for (auto x : v) {

更新 1

$ gcc -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

【问题讨论】:

  • 你使用的是什么版本的 gcc?错误直接来自 gcc。
  • 我添加了我的 gcc 版本,我不确定该版本是否用于编译。我会试试你的建议。
  • 我更新了它,有一个错字。您可以通过运行g++ --version 查看当前版本

标签: c++ c++11 visual-studio-code


【解决方案1】:

听起来像是旧版本的 gcc。你应该升级!

你可以通过改变来让它工作

"args": ["-O2", "-g", "test.cpp"],

"args": ["-std=c++11", "-O2", "-g", "test.cpp"],

但是,真的,考虑升级。

参考:https://gcc.gnu.org/gcc-4.8/cxx0x_status.html

【讨论】:

  • 成功了!谢谢你。我会在 5 分钟后接受你的回答。
  • 嗨,Aluan,你的旧帖子,但你是如何升级的?我正在使用 Visual Studio Code,它正在使用 clang,我在终端中使用 g++。从两者我得到上面的错误。但是当我尝试升级编译器 g++ 时,它说它是最新的。以及如何通过 Visual Studio Code 更新 clang?任何帮助表示赞赏。
  • 抱歉,我不熟悉那个特定的工具链。我希望您可以查看“如何升级 Clang”,但我从未这样做过。请注意,如果您在 IDE 和命令行中使用不同的编译器,您可能会遇到问题
猜你喜欢
  • 2021-07-11
  • 2016-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多