我是 C++ 和 Visual Studio Code 的新手,所以我不确定如何解决这个问题。我试过在 Using Clang in Visual Studio Code 中完成教程,但我似乎无法在没有 iostream、vector 和string 错误。 我还看到这里的帖子指出我应该更改配置中的
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${workspaceFolder}
。您需要将路径添加到您的系统目录。运行此命令并确保打印出的所有路径都列在您的
c_cpp_properties.json文件中:
gcc -v -E -x c++ -
c_cpp_properties.json
文件中改3次即可
检查你的 C++ 版本
c++ -v
检查目录
/usr/include/c++
,如果缺少相应的版本(10/或11/或12/),请安装它
sudo apt install libstdc++-XX
。我必须安装libstdc++-12
sudo apt install libstdc++-12
然后我意识到,当我命名文件时,我只将它命名为
helloworld而不是 helloworld.cpp 因为我一直在使用的其他 IDE 不需要它。