18

I am in the process of learning c++ and I'm using visual studio code for Mac. I use Code Runner to run my program. My problem is that when I use something from c++11 like "auto" for variable declaration, visual studio code gives me a warning like this, but if I try running it on Xcode or Eclipse it doesn't:

warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
for(auto y: nstrVec)

This is the program if it's necessary:

#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <numeric>
#include <sstream>

int main(){

std::vector<std::string> nstrVec(10);

std::string str("I'm a string");
nstrVec[0] = str;

std::cout << str.at(0) << "\n";
std::cout << str.front() << " " << str.back() << "\n";
std::cout << "Length " << str.length() << "\n";
// copies all characters after the fourth 
std::string str2(str, 4);

for(auto y: nstrVec)
    if(y != "")
        std::cout << y << "\n";

return 0;
}

And this is the c_cpp_proprerties.json file:

{
"configurations": [
    {
        "name": "Mac",
        "includePath": [
            "${workspaceFolder}/**",
                 "/System/Library/Frameworks/Kernel.framework/Versions/A/Headers"
        ],
        "defines": [],
        "macFrameworkPath": [
            "/System/Library/Frameworks",
            "/Library/Frameworks"
        ],
        "compilerPath": "/usr/bin/clang",
        "cStandard": "c11",
        "cppStandard": "c++17",
        "intelliSenseMode": "clang-x64"
    }
],
"version": 4
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
BONANDRINI CARLO
  • 215
  • 1
  • 2
  • 7
  • Are you compiling for C++11? It doesn't sound like it. The `auto` keyword was introduced in C++11 so before that it was considered a language extension by Visual Studio. – Cory Kramer Jun 26 '18 at 15:46
  • @CoryKramer Question is about VS Code, not VS. –  Jun 26 '18 at 15:47
  • You are telling the compiler to use 2 different standards: you have both "-std=c++17" and "-std=c++11". – Fabio says Reinstate Monica Jun 26 '18 at 15:47
  • @FabioTurati I tried using only one but it didn't work anyway – BONANDRINI CARLO Jun 26 '18 at 16:01
  • 4
    Can you compile it from the command line? That is, open a shell, go to that directory, and type `g++ -std=c++17 -g helloworld.cpp -o helloworld`: does it work? – Fabio says Reinstate Monica Jun 26 '18 at 16:14
  • @FabioTurati clang: error: no such file or directory: 'helloworld.cpp' clang: error: no input files – BONANDRINI CARLO Jun 26 '18 at 16:40
  • Your compiler path is `"/usr/bin/clang"`, but then in task you have `"command": "g++"`. Which compiler have you installed? – Bob__ Jun 26 '18 at 16:51
  • @Bob__ i have installed Xcode that should install clang, but I have tried changing command to clang but it doesn't change anything – BONANDRINI CARLO Jun 26 '18 at 16:55
  • ***error: no such file or directory*** You were in the wrong folder when you executed that. Your shell must be in the same folder as `helloworld.cpp` – drescherjm Jun 26 '18 at 16:56
  • the thing is that I don't use task to run the program, I use the extension code runner to run it. – BONANDRINI CARLO Jun 26 '18 at 16:57
  • This https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner ? What does its configuration looks like? – Bob__ Jun 26 '18 at 17:00
  • @Bob__ // do you mean this: Set the executor of each language "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt" – BONANDRINI CARLO Jun 26 '18 at 17:19
  • Well, IF you are using that extension to run the program (why?), in the command line, after `g++` (clang?) add `-std=c++17`. – Bob__ Jun 26 '18 at 18:00
  • @Bob__ THANK YOU THAT FINALLY SOLVED THE PROBLEM – BONANDRINI CARLO Jun 26 '18 at 18:27
  • 2
    I'm glad that you could solve your problem, but please do not edit your post to add "SOLVED" in the title, Stack Overflow works differently. The correct way to indicate it is to accept an answer, once there is one. You could ask @Bob__ whether he is interested in posting one; if he isn't, please post it yourself and accept it. Thank you! – Fabio says Reinstate Monica Jun 26 '18 at 22:10

11 Answers11

26

In VS Code:

File>>Preference>>Settings>>Extensions

find C_Cpp>Default:Cpp Standard drop down menu

set that to c++11

Image of Option Window

user1781290
  • 2,674
  • 22
  • 26
iamczar
  • 285
  • 3
  • 2
17

I spent so long today trying to figure out why I was getting this error and no where had the exact answer I required so I thought I'd post it here just in case I can save anyone the hassle.

If you're using code runner, look in user settings and set:

 "code-runner.executorMap" : { "cpp" : "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt" }

The pertinent bit being "g++ -std=c++17".

This is providing of course you can compile your programme in shell using Daniel's solution above but not in VScode + and using code runner.

Ashkan S
  • 10,464
  • 6
  • 51
  • 80
ljelliot
  • 219
  • 2
  • 8
13

I had the same problem, but solved it using set vscode-user-settings <>

"clang.cxxflags": ["-std=c++14"]

vscode- user setting

Filnor
  • 1,290
  • 2
  • 23
  • 28
vic.zhang
  • 139
  • 1
  • 6
9

For everyone who comes to this question to find a quick answer (like I did):

The following compiler command should compile your program main.cpp with the latest C++ standard (c++17) and should get rid of warning messages like the one described above:

g++ -std=c++17 -g main.cpp -o main

It is mentioned multiple times in the comments, but I think this question should have a regular answer.

Daniel Schütte
  • 578
  • 1
  • 6
  • 20
8

I used this to solve my problem. Open your terminal

bash

echo "alias g++='g++ -std=c++17'" >> ~/.bashrc
source ~/.bashrc

zsh

echo "alias g++='g++ -std=c++17'" >> ~/.zshrc
source ~/.zshrc
TimingTone
  • 89
  • 1
  • 2
2

Fix for MAC + code runner.

  1. Select Code -> Settings -> Settings

  2. In the search prompt, seach "code-runner":

enter image description here

  1. Click on "Edit settings.json"

  2. Look for a field called "code-runner.executorMap" -> "cpp"

  3. After g++, add the following to it " -std=c++17 ". In other words, the line should look something like this:

    "cpp": "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"

  4. Close VSCode, and open it again.

Dennis Kozevnikoff
  • 2,078
  • 3
  • 19
  • 29
1

If you're using CPH judge extension in VS add -std=c++11 in Cph › Language › Cpp: Args in extension settings

1

None of the answers here worked for me on Mac that were entirely within VSCode (I didn't want to modify my .zshrc file).

What did work though, was adding argument --std=c++20 for the clangd: Fallback Flags under Extensions > clangd, then restarting VSCode.

enter image description here

jtlim
  • 3,861
  • 1
  • 16
  • 14
0

If you're using CPH, add this line to Cph >> Language >> Cpp: Args

-std=c++17

If it doesn't work for you, also go to File >> Preference >> Settings >> Extensions >> C_Cpp >> Default:Cpp_Standard and set that to c++17

  • why not just edit [SHASHANK BHARDWAJ's answer](https://stackoverflow.com/a/70536529/11107541)? This is almost a duplicate. – starball Sep 03 '22 at 20:49
0

I managed to fix the issue by running the VSCode clangd extension's "Download language server" command.

Open command palette by Ctrl/Cmd + Shift + P, then search "clangd: Download language server" and run the command. Then restart vscode. The warnings should disappear.

You could also try "clangd: Check for language server update"

Here's the command:

S.B
  • 13,077
  • 10
  • 22
  • 49
-2

Mac users can follow this YT video, It worked for me: CPH Warning 'auto' type specifier is a C++11 | Fix this error now!!

Go to Manage -> settings -> type code-runner in search settings -> add this property at the end of file "cph.language.cpp.Args": "-std=c++17"

Checkout the below images for more clarifications

Image 1

Image 2

user16217248
  • 3,119
  • 19
  • 19
  • 37