简体   繁体   English

Visual Studio Code c++11 扩展警告

[英]Visual Studio Code c++11 extension warning

I am in the process of learning c++ and I'm using visual studio code for Mac.我正在学习 c++ 并且正在使用适用于 Mac 的 Visual Studio 代码。 I use Code Runner to run my program.我使用 Code Runner 来运行我的程序。 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:我的问题是,当我使用 c++11 中的某些东西(例如“auto”)进行变量声明时,visual studio 代码会给我这样的警告,但是如果我尝试在 Xcode 或 Eclipse 上运行它,它不会:

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:这是 c_cpp_proprerties.json 文件:

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

In VS Code:在 VS 代码中:

File>>Preference>>Settings>>Extensions文件>>首选项>>设置>>扩展

find C_Cpp>Default:Cpp Standard drop down menu找到 C_Cpp>Default:Cpp Standard 下拉菜单

set that to c++11将其设置为 c++11

选项窗口的图像

I had the same problem, but solved it using set vscode-user-settings <>我遇到了同样的问题,但使用 set vscode-user-settings <> 解决了它

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

vscode-用户设置

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".相关位是“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.这当然提供了您可以使用上面 Daniel 的解决方案在 shell 中编译您的程序,但不能在 VScode + 中使用代码运行器编译。

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:下面的编译器命令应该使用最新的 C++ 标准 (c++17) 编译你的程序main.cpp并且应该去掉上面描述的警告消息:

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.评论中多次提到,但我认为这个问题应该有一个常规的答案。

I used this to solve my problem.我用这个来解决我的问题。 Open your terminal打开你的终端

bash猛击

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

zsh zsh

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

如果您在 VS 中使用 CPH 判断扩展,请在 Cph › 语言 › Cpp:扩展设置中的 Args 中添加-std=c++11

We only have to link g++ compiler with g++-11 compiler.我们只需要将 g++ 编译器与 g++-11 编译器链接起来。 Below is the process how to do it on macbook.以下是如何在 macbook 上执行此操作的过程。

For solution click here解决方案点击这里

If this not works then refer to this video: https://youtu.be/CZ7Mf7qxbIU如果这不起作用,请参阅此视频: https://youtu.be/CZ7Mf7qxbIU

If you're using CPH, add this line to Cph >> Language >> Cpp: Args如果您使用的是 CPH,请将此行添加到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如果它不适合你,go 到File >> Preference >> Settings >> Extensions >> C_Cpp >> Default:Cpp_Standard并将其设置为 c++17

None of the answers here worked for me on Mac that were entirely within VSCode (I didn't want to modify my.zshrc file).在 Mac 上,这里没有一个答案对我有用,因为它们完全在 VSCode 中(我不想修改 my.zshrc 文件)。

What did work though, was adding argument --std=c++20 for the clangd: Fallback Flags under Extensions > clangd , then restarting VSCode.不过,确实有效的是为 clangd 添加参数--std=c++20Extensions > clangd下的clangd: Fallback Flags ,然后重新启动 VSCode。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM