vscode 无法打开源文件“iostream”

时间:2020-12-23 08:27:31

标签: c++ macos visual-studio-code computer-science include-path

我是 C++ 和 vscode 的新手,所以我不知道如何解决这个问题。我已经尝试过 https://code.visualstudio.com/docs/cpp/config-clang-mac 上的教程,但我似乎无法在没有 iostream、vector 和 string 错误的情况下粘贴他们的“hello world”代码。我还在这里看到了一些帖子,表明我应该在配置中更改 includePath 字符串数组,但是将路径添加到我的项目文件夹似乎并没有消除这些错误。我在这里不知所措,因为到目前为止我所看到的一切仍然无法正常工作。

导致这些错误的代码已直接从上面的教程中复制粘贴:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
    
    for (const string& word : msg)
    {
        cout << word << " ";
    }
    cout << endl;
}

在前三行中,我收到错误 "cannot open source file {the included import's name}"。编译器声明 #include errors detected. Please update your includePath. Squiggles are disabled for this translation unit (/Users/rjc/projects/helloworld/helloworld.cpp).C/C++(1696)

我在我的 includePath 中添加了这一行(配置页面声明每行添加一个 includePath,因此我将这两个语句放在单独的行中): ${workspaceFolder}/** /Users/rjc/projects/helloworld/

然而,这并没有减少错误的数量。我不确定在这里做什么,因为教程似乎对我不起作用。

我正在运行 macOS Big Sur (11.1) 和 clang 版本 12.0.0。如果您有任何关于我为什么会遇到这些问题的直觉,我将不胜感激。

2 个答案:

答案 0 :(得分:4)

您的 includePath 只有 ${workspaceFolder}。您需要将路径添加到您的系统目录。运行此命令并确保打印出的所有路径都列在您的 c_cpp_properties.json 中:gcc -v -E -x c++ -

答案 1 :(得分:0)

确保 compilerPath 设置中的编译器存在并且它支持 C++。

我在没有 C++ cpp 支持的情况下安装了 gcc 的 (centos) 系统上看到这些 stdlib 包含错误。安装丢失的打包修复了 vscode 中的错误。未能从终端编译 hello.cpp 表明我的根本问题不在 vscode。