vscode 无法打开源文件“iostream”

rjc*_*810 2 c++ macos computer-science include-path visual-studio-code

我是 C++ 和 vscode 的新手,所以我不确定如何解决这个问题。我试图通过https://code.visualstudio.com/docs/cpp/config-clang-mac 上的教程,但如果没有 iostream、vector 和字符串错误。我还在这里看到了一些帖子,指出我应该在配置中更改 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;
}
Run Code Online (Sandbox Code Playgroud)

在前三行中,我收到了错误"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。对于我为什么会遇到这些问题的任何直觉,我将不胜感激。

All*_*ZHU 5

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

  • 我将所有路径复制粘贴到 properties.json 中的 includePath 数组中,但仍然遇到相同的错误。我有“${workspaceFolder}/**”、“/usr/local/include”、“/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1”、“/Library/Developer/CommandLineTools /usr/lib/clang/12.0.0/include", "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include", "/Library/Developer/CommandLineTools/usr/include", "/Library/开发人员/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks” (2认同)

小智 5

如上所述,你应该

  • 在终端中运行此命令: gcc -v -E -x c++ -
  • 然后它会显示所有要包含的路径,如图所示

在此输入图像描述

  • 现在在 vs code 中打开搜索此文件c_cpp_properties.json(通过cntrl + p在 vscode 中点击)
  • 现在打开c_cpp_properties.json文件并粘贴我标记的所有文件路径(在您的情况下可能或多或少),如图所示

在此输入图像描述

  • 然后你就可以出发了!
  • 快乐编码!