c++ - Visual Studio Code : code not running for C++11

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

当我尝试运行这段代码并按下右上角的播放按钮时:

enter image description here

#include <iostream>
using namespace std;


void test()
{

int v[]={0,1,2,3};

for(auto x:v)
    cout << x << '\n';
}

int main()
{

}

我收到两个警告:

enter image description here

1p7.cpp:10:5: 警告:'auto' 类型说明符是 C++11 扩展 [-Wc++11-extensions] 对于(自动 x:v) ^ 1p7.cpp:10:11: 警告:基于范围的 for 循环是 C++11 扩展 [-Wc++11-extensions] 对于(自动 x:v) ^ 生成了 2 个警告。

当我通过终端运行代码时,我得到:

1p7.cpp:10:5: warning: 'auto' type specifier is a C++11 extension
      [-Wc++11-extensions]
for(auto x:v)
    ^
1p7.cpp:10:11: warning: range-based for loop is a C++11 extension
      [-Wc++11-extensions]
for(auto x:v)
          ^
2 warnings generated.

同样的事情。我将 g++ 用于终端,但我相信 clang 用于 VS 代码。

我已经将设置更改为:

enter image description here

然后我在终端上运行了 brew update 和 brew upgrade。 g++ -version 给出:

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

tasks.json 是:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "msbuild",
            "args": [
                // Ask msbuild to generate full paths for file names.
                "/property:GenerateFullPaths=true",
                "/t:build",
                // Do not generate summary otherwise it leads to duplicate errors in Problems panel
                "/consoleloggerparameters:NoSummary"
            ],
            "group": "build",
            "presentation": {
                // Reveal the output only if unrecognized errors occur.
                "reveal": "silent"
            },
            // Use the standard MS compiler pattern to detect errors, warnings and infos
            "problemMatcher": "$msCompile"
        }
    ]
}

感谢任何帮助。这真是令人沮丧。我可以在终端中使用 std=c++11 来运行代码,但我宁愿让这个 1) 自动运行而不添加它,每次我有一个新程序 2) 希望它在 VS 代码中工作。

最佳答案

构建

clang 需要 -std=c++11 选项。 Clang c++11

g++ 将接受 -std=c++11-std=gnu++11 Gnu C++ standards support

执行测试()

您需要在 main 中调用函数测试才能执行测试。

int main()
{
  test();
}

备注:

我使用带有 CMakeLists.txt 的 cmake 来构建我的项目。

关于c++ - Visual Studio Code : code not running for C++11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59193768/

相关文章:

c++ - 多次创建新结构

c++ - 在go中删除内存

c++ - 已知参数的模板推导失败

C++ 错误 : expected a '{'

c++ - 如何检查哪些项目依赖于 .cpp 文件?

c++ - VC++ 自动说明符假定 vector<bool>::back 的引用限定符

c++ - 基于范围的循环 : Auto changes meaning in C++11

keyboard-shortcuts - VS Code中选择单词和扩展选择的快捷键

windows - 清除 Visual Studio Code 的 workspaceStorage 文件夹

reactjs - TypeScript 可选属性不接受未定义的值