BON*_*RLO 8 c++ macos visual-studio-code
我正在学习c ++,正在使用Mac的Visual Studio代码。我使用Code Runner运行程序。我的问题是,当我使用c ++ 11中的诸如“ auto”之类的变量声明时,Visual Studio代码会给我这样的警告,但是如果我尝试在Xcode或Eclipse上运行它,则不会:
warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
for(auto y: nstrVec)
这是必要的程序:
#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;
}
这是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
}
小智 17
在 VS 代码中:
文件>>首选项>>设置>>扩展
找到 C_Cpp>Default:Cpp Standard 下拉菜单
将其设置为 c++11

Dan*_*tte 10
对于每个提出这个问题想要快速找到答案的人(就像我一样):
以下编译器命令应main.cpp使用最新的 C++ 标准 (c++17) 编译您的程序,并应消除如上所述的警告消息:
g++ -std=c++17 -g main.cpp -o main
评论中多次提到,但我认为这个问题应该有一个常规的答案。
我今天花了很长时间试图弄清楚为什么我会收到这个错误,但没有我需要的确切答案,所以我想我会把它贴在这里以防万一我可以省去麻烦。
如果您使用的是代码运行器,请查看用户设置并设置:
 "code-runner.executorMap" : { "cpp" : "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt" }
相关位是“g++ -std=c++17”。
这当然提供了您可以使用上面 Daniel 的解决方案在 shell 中编译您的程序,但不能在 VScode + 中使用代码运行器编译您的程序。
我用这个来解决我的问题。打开你的终端
巴什
echo "alias g++='g++ -std=c++17'" >> ~/.bashrc
source ~/.bashrc
桀骜
echo "alias g++='g++ -std=c++17'" >> ~/.zshrc
source ~/.zshrc
修复了 MAC + 代码运行程序。
选择代码 -> 设置 -> 设置
在搜索提示中,搜索“code-runner”:
单击“编辑settings.json”
查找名为“code-runner.executorMap”的字段 -> “cpp”
在 g++ 之后,添加以下内容“ -std=c++17 ”。换句话说,该行应该如下所示:
"cpp": "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
关闭 VSCode,然后再次打开。
小智 6
我有同样的问题,但是使用set vscode-user-settings <>解决了
"clang.cxxflags": ["-std=c++14"]

| 归档时间: | 
 | 
| 查看次数: | 4940 次 | 
| 最近记录: |