mikanim
mikanim

Reputation: 439

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

When I try to run this code and press the play button on the top right:

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()
{

}

I get two warnings:

enter image description here

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.

When I run the code via terminal I get:

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.

The same thing. I used g++ for terminal but I believe clang is used for VS code.

I already changed the setting to:

enter image description here

and I ran brew update and brew upgrade on the terminal. g++ -version gives:

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 is:

{
    // 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"
        }
    ]
}

Any help is appreciated. This is really frustrating. I can use the std=c++11 in the terminal to make the code run but I would rather have this 1) automatically run without adding that every time I have a new program 2) would like it to work in VS code.

Upvotes: 0

Views: 3152

Answers (1)

Arthur Anderson
Arthur Anderson

Reputation: 152

Build

clang will need the -std=c++11 option. Clang c++11

g++ will accept -std=c++11 or -std=gnu++11 Gnu C++ standards support

Executing test()

You will need to call function test within main for the test to execute.

int main()
{
  test();
}

Note:

I use cmake with a CMakeLists.txt to build my projects.

Upvotes: 3

Related Questions