Visual Studio Code 无法打开源文件“iostream”

问题描述 投票:0回答:8

我是 C++ 和 Visual Studio Code 的新手,所以我不确定如何解决这个问题。我试过在 Using Clang in Visual Studio Code 中完成教程,但我似乎无法在没有 iostreamvectorstring 错误。 我还看到这里的帖子指出我应该更改配置中的

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 检测到错误。请更新您的 includePath。此翻译单元 (/Users/rjc/projects/helloworld/helloworld.cpp).C/C++(1696)
禁用了波浪线

我在我的 includePath 中添加了这一行(配置页面声明每行添加一个 includePath,所以我将这两个语句放在不同的行中):

${workspaceFolder}/**

/Users/rjc/projects/helloworld/
但这并没有减少错误的数量。我不太确定在这里做什么,因为教程似乎对我不起作用。

我正在运行带有 Clang 版本 12.0.0 的

macOS 11.1

(Big Sur)。为什么我有这些问题?

c++ macos visual-studio-code computer-science include-path
8个回答
17
投票
${workspaceFolder}

。您需要将路径添加到您的系统目录。运行此命令并确保打印出的所有路径都列在您的

c_cpp_properties.json
文件中:gcc -v -E -x c++ -
    


0
投票
c_cpp_properties.json

文件中改3次即可


    “名称”:“Mac”
  1. 将“usr/include/linux”添加到“includePath”
  2. “intelliSenseMode”:“clang-x64”
  3. 此解决方案仅适用于 Linux 用户。


0
投票
compilerPath

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

我看到这些 

stdlib

包括在 (CentOS) 系统上的错误,该系统安装了 GCC 而没有 C++ 支持。安装丢失的包修复了 Visual Studio Code 中的错误。未能从终端编译 hello.cpp 清楚地表明我的根本问题不在 Visual Studio Code 中。


0
投票

检查你的 C++ 版本

c++ -v

检查目录
/usr/include/c++

,如果缺少相应的版本(10/或11/或12/),请安装它

sudo apt install libstdc++-XX
。我必须安装
libstdc++-12
sudo apt install libstdc++-12



0
投票


0
投票


0
投票

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

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

然后你就可以开始了!
  • 编码愉快!

-2
投票

然后我意识到,当我命名文件时,我只将它命名为

helloworld

而不是 helloworld.cpp 因为我一直在使用的其他 IDE 不需要它。

© www.soinside.com 2019 - 2024. All rights reserved.