Eclipse CDT 显示语义错误,但编译没问题

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

Eclipse CDT shows semantic errors, but compilation is ok

eclipsegcceclipse-cdt

提问by fasked

I have installed Eclipse Indigo for C/C++ Linux developers on Ubuntu 10.04 x86.

我已经在 Ubuntu 10.04 x86 上为 C/C++ Linux 开发人员安装了 Eclipse Indigo。

When I use common predefined macro __BASE_FILE__Eclipse says Symbol '__BASE_FILE__' could not be resolved, but compilation is OK. I have to use it so often in my code and Eclipse fills my screen with red lines and bug icons :)

当我使用通用预定义宏时,__BASE_FILE__Eclipse 说Symbol '__BASE_FILE__' could not be resolved,但编译没问题。我必须在我的代码中经常使用它,而 Eclipse 用红线和错误图标填充了我的屏幕:)

How can I fix this?

我怎样才能解决这个问题?

回答by taloft

Not sure if this addresses your specific problem, but I also had semantic errors. They just came out of the blue after having a working project.

不确定这是否解决了您的具体问题,但我也有语义错误。他们只是在完成一个工作项目后突然出现。

Anyway, I fixed it with a single option in workspace settings by setting: "Build configuration for indexer: Use active build configuration"

无论如何,我通过设置在工作区设置中使用单个选项修复了它:“为索引器构建配置:使用活动构建配置”

The other option was "Use the build configuration specified in the project's indexer settings" and this is the one that wasn't working. I think it may have been corrupted after an eclipse crash.

另一个选项是“使用项目索引器设置中指定的构建配置”,这是一个不起作用的选项。我认为它可能在日食崩溃后已损坏。

回答by WindRider

Window -> Preferences -> C/C++ -> Code Analysis -> Syntax and Semantic Errors: OFF

窗口 -> 首选项 -> C/C++ -> 代码分析 -> 语法和语义错误:关闭

This won't solve the cause of the problem but at least will stop the false errors from being shown.

这不会解决问题的原因,但至少会阻止显示错误的错误。

回答by Zazun

The simplest solution is to reset the indexer:

最简单的解决方案是重置索引器:

Window / Preferences / C/C++ / Indexer.
uncheck "Enable indexer" ->>OK
rebuild all, may show lots of errors
check "Enable indexer" ->>OK
rebuild all

窗口/首选项/C/C++/索引器。
取消选中“启用索引器”->>确定
重建所有,可能会显示很多错误
检查“启用索引器”->>确定
重建所有

This error can be produced by forced closing of eclipse by power failure.

此错误可能是由于电源故障而强制关闭 eclipse 产生的。

回答by mczraf

You need to rebuild the index of your project.

您需要重建项目的索引。

Right-click on the project, then Index->Rebuild.

右键单击项目,然后索引->重建。

回答by Tanghuihua

Deleting the .metadata folder , and then Import project .

删除 .metadata 文件夹,然后导入项目。

This way is OK!

这个方法可以!

回答by sidney_h

I think it has something to do with the workspace/.metadata. I had the problem of semantic errors reported but Hello World compiles and runs. I deleted the project, created another one, same error reporting. Reinstalled CDT, same thing. Deleted the workspace, shut down Eclipse, restarted, created new hello world, same thing. Deleted the workspace again, shut down Eclipse again, this time noticed that Eclipse had recreated the workspace folder during shutdown when it couldn't find it. Deleted the workspace with Eclipse shut down. Restarted Eclipse, created HW project, errors show for about a second and then gone. Deleted project, created another one, errors show for a sec, then poof. If you actually had projects in your workspace (mine was a clean install), I bet that you could just delete the .metadata folder within the workspace, and it would fix it.

我认为这与工作区/.metadata 有关系。我报告了语义错误的问题,但 Hello World 编译并运行。我删除了该项目,创建了另一个,同样的错误报告。重新安装CDT,同样的事情。删除工作区,关闭 Eclipse,重新启动,创建新的 hello world,同样的事情。再次删除工作区,再次关闭Eclipse,这一次发现Eclipse在关闭时找不到工作区文件夹,重新创建了工作区文件夹。在 Eclipse 关闭的情况下删除了工作区。重新启动 Eclipse,创建硬件项目,错误显示大约一秒钟然后消失。删除项目,创建另一个项目,错误显示一秒钟,然后噗。如果您的工作区中确实有项目(我的是全新​​安装),我敢打赌您可以删除工作区中的 .metadata 文件夹,

回答by ondrisko

Updated: You have to add proper paths to Project->Propoerties->C/C++ General->Paths and Symbols. If it does not help, you can configure Eclipse Code Analyser (which generates the 'errors') in Project->Properties->C/C++ General->Code Analysis. Under the error description you have problem with, try Customize Selected->Scope->Exclusion Patterns.

更新:您必须将正确的路径添加到 Project->Propoerties->C/C++ General->Paths and Symbols。如果没有帮助,您可以在 Project->Properties->C/C++ General->Code Analysis 中配置 Eclipse 代码分析器(它会生成“错误”)。在您遇到问题的错误描述下,尝试自定义选定->范围->排除模式。

回答by compostus

IMO the correct solution is to define the symbol in Project -> Properties->C/C++ General -> Paths and Symbols -> Symbols tab. Since you can assume the symbol will always be supplied by the compiler, you just tell Eclipse to consider it defined.

IMO 正确的解决方案是在 Project -> Properties->C/C++ General -> Paths and Symbols -> Symbols tab 中定义符号。由于您可以假设该符号将始终由编译器提供,您只需告诉 Eclipse 将其视为已定义。

I had the same problem just recently, where Eclipse complained about undefined macro, that i always supply using -D<MACRO_NAME>compiler option.

我最近遇到了同样的问题,Eclipse 抱怨未定义的宏,我总是使用-D<MACRO_NAME>编译器选项提供。

回答by fluke

I personally agree with compostus' answer.

我个人同意 compostus 的回答。

It's good to solve the real problem that eclipse's code analyzer have.

很好的解决了eclipse的代码分析器存在的实际问题。

But when I try this steps: define the symbol in Project -> Properties->C/C++ General -> Paths and Symbols

但是当我尝试这个步骤时:在 Project -> Properties->C/C++ General -> Paths and Symbols 中定义符号

I don't find symbol tab or any place to add a symbol. what I want to do is putting macros like -DANDROID or #define ANDROID 1 to eclipse so that the code analyzer can find this definition.

我没有找到符号选项卡或任何添加符号的地方。我想要做的是将像 -DANDROID 或 #define ANDROID 1 这样的宏放到 eclipse 中,以便代码分析器可以找到这个定义。

I'm under Mac OSX mountain lion.

我在 Mac OSX 山狮下。

回答by mike

adding as another answer, hopefully this will help someone.

添加作为另一个答案,希望这会对某人有所帮助。

I have a ~simple workspace (1 c++ shared-object (linux's version of a DLL, 3 c++ executables, and a pydev python project)

我有一个 ~simple 工作区(1 个 c++ 共享对象(linux 版本的 DLL、3 个 c++ 可执行文件和一个 pydev python 项目)

I checked out all the code to a new machine, and it builds fine but had numerous 'semantic errors' on std-c and std-C++ code and includes.. I went through all the discovery / indexer setting to no avail.

我将所有代码检查到一台新机器上,它构建得很好,但在 std-c 和 std-C++ 代码上有许多“语义错误”,包括……我经历了所有的发现/索引器设置,但无济于事。

==> deleting the entire .metadata folder fixed this.

==> 删除整个 .metadata 文件夹修复了这个问题。

Since i had nothing non-trivial set up in the workspace (all in the project files / makefiles), eclipse happily created a fresh workspace, and i simply had to do a file->import-> existing projects

由于我在工作区中没有设置任何重要的东西(都在项目文件/makefile 中),eclipse 愉快地创建了一个新的工作区,我只需要做一个文件-> 导入-> 现有项目