'Visual Studio Code c++11 extension warning
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
}
Solution 1:[1]
In VS Code:
File>>Preference>>Settings>>Extensions
find C_Cpp>Default:Cpp Standard drop down menu
set that to c++11
Solution 2:[2]
I had the same problem, but solved it using set vscode-user-settings <>
"clang.cxxflags": ["-std=c++14"]
Solution 3:[3]
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.
Solution 4:[4]
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
Solution 5:[5]
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.
Solution 6:[6]
If you're using CPH judge extension in VS add -std=c++11 in Cph › Language › Cpp: Args in extension settings
Solution 7:[7]
We only have to link g++ compiler with g++-11 compiler. Below is the process how to do it on macbook.
If this not works then refer to this video : https://youtu.be/CZ7Mf7qxbIU
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | user1781290 |
Solution 2 | Filnor |
Solution 3 | Ashkan S |
Solution 4 | |
Solution 5 | Daniel Schütte |
Solution 6 | SHASHANK BHARDWAJ |
Solution 7 | Jatin Ranpariya |