Eclipse for C++ 中的“字符串无法解析”错误(Eclipse 无法解析标准库)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7905025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:41:46  来源:igfitidea点击:

"string could not resolved" error in Eclipse for C++ (Eclipse can't resolve standard library)

c++eclipse

提问by code511788465541441

I have the following code in eclipse for c++ and it's underlining stringand coutand saying could not be resolved.

我在 Eclipse 中有以下 C++ 代码,它带有下划线stringcout并说无法解决。

#include <string>
#include <iostream>

using namespace std;


int main()
{
    string s;
    s = "hello world";
    cout << s;
    return 0;

}

Anyone know why?

有谁知道为什么?

edit: screenshot

编辑:截图

Edit:I have found a solution thanks everyone (see answers).

编辑:我找到了一个解决方案,谢谢大家(见答案)。

enter image description here

在此处输入图片说明

采纳答案by code511788465541441

The problem was that I needed to have both minGW and MSYS installed and added to PATH.

问题是我需要同时安装minGW 和 MSYS 并将其添加到 PATH。

The problem is now fixed.

现在问题已解决。

回答by Vanuan

I've also had this issue.

我也遇到过这个问题。

I've found out that it is because Eclipse couldn't find all include headers.

我发现这是因为 Eclipse 无法找到所有包含标头。

Easy fix:

轻松修复:

This simple and quick solution might fix your problem (for example, when the Eclipse project was moved to a different location on disk, then imported again in Eclipse), if not, jump to the next section (Detailed fix).

这个简单而快速的解决方案可能会解决您的问题(例如,当 Eclipse 项目被移动到磁盘上的不同位置,然后在 Eclipse 中再次导入时),如果没有,请跳到下一部分(详细修复)。

  1. Go to project > properties > C/C++ Build> Tool Chain Editor
  2. Change the Current toolchainto any other value, click Apply
  3. Set the Current toolchainto the original value, click Apply
  4. Compile your project
  1. 转到项目 > 属性 > C/C++ 构建>工具链编辑器
  2. 当前工具链更改为任何其他值,单击应用
  3. 当前工具链设置为原始值,单击应用
  4. 编译你的项目

Detailed fix:

详细修复:

Before proceeding check if your toolchain is properly installed.

在继续之前检查您的工具链是否已正确安装。

  1. Switch to a new workspace.
  2. Remove .cproject file and the ".settings" folder
  3. Import your project as Makefile project (or just create a new if you prefer CDT Build system)
  4. Go to project-> properties->C/C++ Build->Toolchain editor. Choose your toolchain.
  5. Press project->Index->Rebuild
  6. If the problem isn't resolved, change system language to English and try the above steps again.
  1. 切换到新工作区。
  2. 删除 .cproject 文件和“.settings”文件夹
  3. 将您的项目导入为 Makefile 项目(或者,如果您更喜欢 CDT 构建系统,则只需创建一个新项目)
  4. 转到项目-> 属性-> C/C++ 构建-> 工具链编辑器。选择您的工具链。
  5. 按项目->索引->重建
  6. 如果问题仍未解决,请将系统语言更改为英文,然后重试上述步骤。

Outdated answer:

过时的答案:

This answer has been outdated. Proceed if nothing of the above helps

这个答案已经过时了。如果以上都没有帮助,请继续

If the previous steps don't help we'll need to setup include directories manually (not recommended though)

如果前面的步骤没有帮助,我们需要手动设置包含目录(虽然不推荐)

  1. Search all unresolved headers using "Right click on Project > Index > Search for unresolved includes".
  2. Search their locations using "find /usr/include/ -name vector -print"
  3. Put include folder paths to "Right click on Project > Properties > C++ General/Path and Symbols/C++"
  4. Run "Right click on Project > Index > Rebuild"
  5. Start from step 1 if there are any unresolved symbols left.
  1. 使用“右键单击项目 > 索引 > 搜索未解析的包含”来搜索所有未解析的标题。
  2. 使用“find /usr/include/ -name vector -print”搜索他们的位置
  3. 将包含文件夹路径设置为“右键单击项目 > 属性 > C++ 常规/路径和符号/C++”
  4. 运行“右键单击项目>索引>重建”
  5. 如果还有任何未解析的符号,则从步骤 1 开始。

回答by code511788465541441

You need to ensure your environment is properly setup in Eclipse so it knows the paths to your includes. Otherwise, it underlines them as not found.

您需要确保您的环境在 Eclipse 中正确设置,以便它知道包含的路径。否则,它会将它们强调为未找到。

回答by Virtual

I've just replied to the related question given by Vanuan (Eclipse CDT: Unresolved inclusion of stl header), and this is my answer :

我刚刚回答了 Vanuan 给出的相关问题(Eclipse CDT:未解析包含 stl 标头),这是我的回答:

You could also try use "CDT GCC Built-in Compiler Settings". Go to the project properties > C/C++ General > Preprocessor Include Path > Providers tab then check "CDT GCC Built-in Compiler Settings" if it is not.

您也可以尝试使用“CDT GCC 内置编译器设置”。转到项目属性 > C/C++ General > Preprocessor Include Path > Providers 选项卡,然后检查“CDT GCC Built-in Compiler Settings”(如果不是)。

None of the other solutions (play with include path, etc) worked for me for the type 'string', but this one fixed it.

其他解决方案(使用包含路径等)都不适用于“字符串”类型,但是这个解决方案修复了它。

回答by Gabor

I had the same problem. Adding include path does work for all except std::string.

我有同样的问题。添加包含路径确实适用于 std::string 之外的所有内容。

I noticed in the mingw-Toolchain many system header files *.tcc

我在 mingw-Toolchain 中注意到许多系统头文件 *.tcc

I added filetype *.tcc as "C++ Header File" in Preferences > C/C++/ File Types. Now std::string can be resolved from the internal index and Code Analyzer. Perhaps this is added to Eclipse CDT by default in feature.

我在首选项 > C/C++/ 文件类型中添加了文件类型 *.tcc 作为“C++ 头文件”。现在可以从内部索引和代码分析器解析 std::string。也许这在特性中默认添加到 Eclipse CDT。

I hope this helps to someone...

我希望这对某人有帮助...

PS: I'm using Eclipse Mars, mingw gcc 4.8.1, Own Makefile, no Eclipse Makefilebuilder.

PS:我使用的是 Eclipse Mars、mingw gcc 4.8.1、自己的 Makefile,没有 Eclipse Makefilebuilder。

回答by Ebru Kaya Kanat

I had the same problem. Change the CurrentBuilder in Properties/C/C++ Build/ToolChainEditor to another value and apply it. Then again change it the original value. It works.

我有同样的问题。将 Properties/C/C++ Build/ToolChainEditor 中的 CurrentBuilder 更改为另一个值并应用它。然后再次将其更改为原始值。有用。

回答by lukee

I had this issue with AOSP (clang).

我在 AOSP (clang) 上遇到了这个问题。

Add external\libcxx\includeto includes and _LIBCPP_COMPILER_CLANGto symbols

添加external\libcxx\include到包含和_LIBCPP_COMPILER_CLANG符号

回答by Basri

Hello here is a simple solution,

您好,这是一个简单的解决方案,

Just go to File -> Convert to a C/C++ Autotools Project Select your project files appropriately.

只需转到 File -> Convert to a C/C++ Autotools Project 适当地选择您的项目文件。

Inclusions will be added to your project file

包含将添加到您的项目文件中