C++ Eclipse CDT:无法解析符号“cout”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10803685/
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-27 14:28:42  来源:igfitidea点击:

Eclipse CDT: Symbol 'cout' could not be resolved

c++eclipseincludeeclipse-cdtinclude-path

提问by Jeff

The error is as above. I have what should be all the necessary files include in the eclipse project:

错误如上。我有 eclipse 项目中应该包含的所有必要文件:

/usr/include/c++/4.6
/usr/include
/usr/include/linux
/usr/local/include

etc.

等等。

I tried std::coutand using namespace std;coutbut it still says unresolved.

我试过了std::coutusing namespace std;cout但它仍然说未解决。

I have imported iostreamand cstdlib.

我有进口iostreamcstdlib

Also, I'm on Ubuntu 12.04 with eclipse 3.7.2.

另外,我在 Ubuntu 12.04 和 Eclipse 3.7.2 上。

Code snippet:

代码片段:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include "XPLMDisplay.h"
#include "XPLMGraphics.h"

int XPluginStart(char * outName,  char * outSig,  char * outDesc) {
    /* ... */
    std::cout << "test" << std::endl;
    /* ... */
}

using namespace std;

使用命名空间标准;



UPDATE: I had created the eclipse project from existing code. Creating a new c++ project fixes it. I'll accept an answer that explains what setting in the existing project could cause this (so I don't have to cut & paste all my projects).

更新:我从现有代码创建了 eclipse 项目。创建一个新的 C++ 项目修复它。我会接受一个解释现有项目中的哪些设置可能导致这种情况的答案(因此我不必剪切和粘贴我的所有项目)。

回答by vitaut

Most likely you have some system-specific include directories missing in your settings which makes it impossible for indexer to correctly parse iostream, thus the errors. Selecting Index -> Search For Unresolved Includesin the context menu of the project will give you the list of unresolved includes which you can search in /usr/includeand add containing directories to C++ Include Paths and Symbolsin Project Properties.

很可能您的设置中缺少一些特定于系统的包含目录,这使得索引器无法正确解析 iostream,从而导致错误。Index -> Search For Unresolved Includes在项目的上下文菜单中进行选择将为您提供未解析的包含列表,您可以在其中搜索/usr/include并将包含目录添加到C++ Include Paths and Symbols项目属性中。

On my system I had to add /usr/include/c++/4.6/x86_64-linux-gnufor bits/c++config.hto be resolved and a few more directories.

在我的系统上,我必须添加/usr/include/c++/4.6/x86_64-linux-gnuforbits/c++config.h待解析和更多目录。

Don't forget to rebuild the index (Index -> Rebuild) after adding include directories.

添加包含目录后,不要忘记重建索引(索引 -> 重建)。

回答by rakslice

To get rid of symbol warnings you don't want, first you should understand how Eclipse CDT normally comes up with unknown symbol warnings in the first place. This is its process, more or less:

要消除您不想要的符号警告,首先您应该了解 Eclipse CDT 通常是如何产生未知符号警告的。这是它的过程,或多或少:

  1. Eclipse detects the GCC toolchains available on the system
  2. Your Eclipse project is configured to use a particular toolchain
  3. Eclipse does discovery on the toolchain to find its include paths and built-in defines, i.e. by running it with relevant options and reading the output
  4. Eclipse reads the header files from the include paths
  5. Eclipse indexes the source code in your project
  6. Eclipse shows warnings about unresolved symbols in the editor
  1. Eclipse 检测系统上可用的 GCC 工具链
  2. 您的 Eclipse 项目被配置为使用特定的工具链
  3. Eclipse 在工具链上进行发现以找到其包含路径和内置定义,即通过使用相关选项运行它并读取输出
  4. Eclipse 从包含路径中读取头文件
  5. Eclipse 为项目中的源代码编制索引
  6. Eclipse 在编辑器中显示有关未解析符号的警告

It might be better in the long run to fix problems with the earlier steps rather than to override their results by manually adding include directories, symbols, etc.

从长远来看,修复早期步骤的问题可能比通过手动添加包含目录、符号等来覆盖它们的结果更好。

Toolchains

工具链

If you have GCC installed, and Eclipse has detected it, it should list that GCC as a toolchain choice that a new C++ project could use, which will also show up in Window -> Preferences -> C/C++ -> New CDT Project Wizardon the Preferred Toolchainstab's Toolchainsbox on the right side. If it's not showing up, see the CDT FAQ's answer about compilers that need special environments(as well as MinGWand Cygwinanswers for the Windows folk.)

如果您安装了 GCC,并且 Eclipse 已检测到它,它应该将该 GCC 列为新 C++ 项目可以使用的工具链选择,它也会显示在右侧Window -> Preferences -> C/C++ -> New CDT Project WizardPreferred Toolchains选项卡Toolchains框中。如果它没有出现,请参阅CDT 常见问题解答关于需要特殊环境的编译器的答案(以及Windows 人员的MinGWCygwin答案。)

If you have an existing Eclipse C++ project, you can change the associated toolchain by opening the project properties, and going to C/C++ Build -> Tool Chain Editorand choosing the toolchain you want from the Current toolchain:pulldown. (You'll have to uncheck the Display compatible toolchains onlybox first if the toolchain you want is different enough from the one that was previously set in the project.)

如果您有一个现有的 Eclipse C++ 项目,您可以通过打开项目属性并C/C++ Build -> Tool Chain EditorCurrent toolchain:下拉列表中转到并选择您想要的工具链来更改关联的工具链。(Display compatible toolchains only如果您想要的工具链与之前在项目中设置的工具链足够不同,您必须先取消选中该框。)

If you added a toolchain to the system after launching Eclipse, you will need to restart it for it to detect the toolchain.

如果您在启动 Eclipse 后向系统添加了工具链,则需要重新启动它以检测工具链。

Discovery

发现

Then, if the project's C/C++ Build -> Discovery Options -> Discovery profiles scopeis set to Per Language, during the next build the new toolchain associated with the project will be used for auto-discovery of include paths and symbols, and will be used to update the "built-in" paths and symbols that show up in the project's C/C++ General -> Paths and Symbolsin the Includesand Symbolstabs.

然后,如果项目的C/C++ Build -> Discovery Options -> Discovery profiles scope设置为Per Language,在下一次构建期间,与项目关联的新工具链将用于自动发现包含路径和符号,并将用于更新显示的“内置”路径和符号达在项目的C/C++ General -> Paths and SymbolsIncludesSymbols标签。

Indexing

索引

Sometimes you need to re-index again after setting the toolchain and doing a build to get the old symbol warnings to go away; right-click on the project folder and go to Index -> Rebuildto do it.

有时您需要在设置工具链并进行构建后再次重新索引以消除旧的符号警告;右键单击项目文件夹并转到Index -> Rebuild执行此操作。

(tested with Eclipse 3.7.2 / CDT 8)

(使用 Eclipse 3.7.2 / CDT 8 测试)

回答by Hyman Kelly

Thanks loads for the answers above. I'm adding an answer for a specific use-case...

感谢您提供上述答案。我正在为特定用例添加答案...

On a project with two target architectures each with its own build configuration (the main target is an embedded AVR platform; the second target is my local Linux PC for running unit tests) I found it necessary to set Preferences -> C/C++ -> Indexer -> Use active build configurationas well as to add /usr/include/c++/4.7, /usr/includeand /usr/include/c++/4.7/x86_64-linux-gnuto Project Properties -> C/C++ General -> Paths and Symbolsand then to rebuild the index.

在具有两个目标架构的一个项目每一个都有自己的生成配置(主要目标是一个嵌入式AVR平台;第二个目标是我的本地Linux的PC运行单元测试),我认为有必要设置Preferences -> C/C++ -> Indexer -> Use active build configuration,以及添加/usr/include/c++/4.7/usr/include/usr/include/c++/4.7/x86_64-linux-gnuProject Properties -> C/C++ General -> Paths and Symbols然后重建索引。

回答by Andy Shiue

I tried the marked solution here first. It worked but it is kind hacky, and you need to redo it every time you update the gcc. I finally find a better solution by doing the followings:

我首先在这里尝试了标记的解决方案。它有效,但它有点hacky,每次更新gcc时都需要重做。我最终通过执行以下操作找到了更好的解决方案:

  1. Project-> Properties-> C/C++ General-> Preprocessor Include Paths, Macros, etc.
  2. Providers-> CDT GCC built-in compiler settings
  3. Uncheck Use global provider shared between projects(you can also modify the global provider if it fits your need)
  4. In Command to get compiler specs, add -std=c++11at the end
  5. Index->Rebuild
  1. Project-> Properties-> C/C++ General->Preprocessor Include Paths, Macros, etc.
  2. Providers-> CDT GCC built-in compiler settings
  3. 取消选中Use global provider shared between projects(如果需要,您还可以修改全局提供程序)
  4. Command to get compiler specs-std=c++11
  5. Index->Rebuild

Voila, easy and simple. Hopefully this helps.

瞧,简单易行。希望这会有所帮助。

Note: I am on Kepler. I am not sure if this works on earlier Eclipse.

注意:我在开普勒。我不确定这是否适用于早期的 Eclipse。

回答by codefool

I had a similar problem with *std::shared_ptr* with Eclipse using MinGW and gcc 4.8.1. No matter what, Eclipse would not resolve *shared_ptr*. To fix this, I manually added the __cplusplus macro to the C++ symbols and - viola! - Eclipse can find it. Since I specified -std=c++11as a compile option, I (ahem) assumed that the Eclipse code analyzer would use that option as well. So, to fix this:

我在使用 MinGW 和 gcc 4.8.1 的 Eclipse 中遇到了与 *std::shared_ptr* 类似的问题。无论如何,Eclipse 都不会解析 *shared_ptr*。为了解决这个问题,我手动将 __cplusplus 宏添加到 C++ 符号和 - viola!- Eclipse 可以找到它。由于我将-std=c++11指定为编译选项,因此我(咳咳)假设 Eclipse 代码分析器也会使用该选项。所以,要解决这个问题:

  1. Project Context -> C/C++ General -> Paths and Symbols -> Symbols Tab
  2. Select C++ in the Languages panel.
  3. Add symbol __cpluspluswith a value of 201103.
  1. 项目上下文 -> C/C++ 常规 -> 路径和符号 -> 符号选项卡
  2. 在语言面板中选择 C++。
  3. 添加值为201103 的符号__cplusplus

The only problem with this is that gcc will complain that the symbol is already defined(!) but the compile will complete as before.

唯一的问题是 gcc 会抱怨符号已经定义(!)但编译会像以前一样完成。

回答by codefool

I am using Ubuntu 12.04 / Eclipse 4.2.1 / CDT 8.1.1 and I used to have the same problem for quite some time: importing a C++ project from SVN would cause these annoying "Unresolved inclusion" errors and I would instead have to create a new project and copy the files in there as a work-around (still partial, since SVN functionality would not be there!).

我正在使用 Ubuntu 12.04 / Eclipse 4.2.1 / CDT 8.1.1 并且我曾经遇到同样的问题很长一段时间:从 SVN 导入 C++ 项目会导致这些烦人的“未解决的包含”错误,而我必须创建一个新项目并将文件复制到那里作为解决方法(仍然是部分的,因为 SVN 功能不存在!)。

At last, I have just found a simple, satisfactory solution:

最后,我刚刚找到了一个简单的,令人满意的解决方案:

  • Go to Project -> Properties -> C/C++ General -> Preprocessor Include Paths, Macros etc. -> Providersand check Enable language settings providers for this project.

  • Restart Eclipse.

  • Project -> Properties -> C/C++ General -> Preprocessor Include Paths, Macros etc. -> Providers检查Enable language settings providers for this project

  • 重新启动 Eclipse。

Hopefully that already does the trick.

希望这已经可以解决问题。

回答by vivi

I simply delete all error in the buttom: problem list. then close project and reopen project clean project build all run

我只是删除按钮中的所有错误:问题列表。然后关闭项目并重新打开项目 clean project build all run

then those stupids errors go.

那么那些愚蠢的错误就会消失。

回答by ormurin

If all else fails, like it did in my case, then just disable annotations. I started a c++11 project with own makefile but couldn't fix all the problems. Even if you disable annotations, eclipse will still be able to help you do some autocompletion. Most importantly, the debugger still works!

如果所有其他方法都失败了,就像我的情况一样,那么只需禁用注释。我用自己的 makefile 启动了一个 c++11 项目,但无法解决所有问题。即使您禁用了注释,eclipse 仍然可以帮助您进行一些自动完成。最重要的是,调试器仍然有效!

回答by WaTho

For me it helped to enable the automated discovery in Properties -> C/C++-Build -> Discovery Options to resolve this problem.

对我来说,它有助于在 Properties -> C/C++-Build -> Discovery Options 中启用自动发现来解决这个问题。

回答by user1205577

I had the same issue using Eclipse CDT (Kepler)on Windowswith Cygwininstalled. After pointing the project properties at every Cygwin include I could think of, it still couldn't find cout.

我使用有同样的问题Eclipse CDT (Kepler)WindowsCygwin安装。在将项目属性指向我能想到的每个 Cygwin 包含之后,它仍然找不到cout.

The final missing piece turned out to be C:cygwin64\lib\gcc\x86_64-pc-cygwin\4.8.2\install-tool\include.

最后丢失的一块原来是C:cygwin64\lib\gcc\x86_64-pc-cygwin\4.8.2\install-tool\include

To sum up:

总结:

  • Right click on the project
  • Choose Properties
  • Navigate to C/C++ General> Paths and Symbols> Includestab
  • Click Add...
  • Click File system...
  • Browse to the location of your Cygwin lib\gcc\x86_64-pc-cygwin\4.8.2\install-tool\include
  • Click OK
  • 右键单击项目
  • 选择 Properties
  • 导航到C/C++ General> Paths and Symbols>Includes选项卡
  • 点击 Add...
  • 点击 File system...
  • 浏览到 Cygwin 的位置 lib\gcc\x86_64-pc-cygwin\4.8.2\install-tool\include
  • 点击 OK

Here is what my project includes ended up looking like when it was all said and done: enter image description here

这是我的项目包含的内容,最终看起来像所有内容: 在此处输入图片说明