Eclipse C/C++ 显示错误但编译?

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

Eclipse C/C++ Shows Errors but Compiles?

c++ceclipsearduino

提问by SuperTron

So I am building some Arduino code in eclipse, as described in Your Second Arduino Project, but every time I use an Arduino library, such as Serial, Eclipse underlines my function names, claiming they cannot be resolved. However, the code actually compiles, so I'm kind of at a loss as to why Eclipse thinks the functions are missing. If anyone has any idea on how to solve this problem it would be appreciated. Thanks beforehand.

因此,我正在 Eclipse 中构建一些 Arduino 代码,如您的第二个 Arduino 项目中所述,但每次我使用 Arduino 库(例如 )时Serial,Eclipse 都会在我的函数名称下划线,声称它们无法解析。但是,代码实际上可以编译,所以我有点不明白为什么 Eclipse 认为这些函数丢失了。如果有人对如何解决这个问题有任何想法,我们将不胜感激。先谢谢了。

EDIT: I should have been more specific, Eclipse underlines the METHODS inside the Arduino libraries. So if I use Serial.println("hello");, it underlines println()and claims it cannot be resolved. Then it compiles just fine and the method works when uploaded to the arduino board.

编辑:我应该更具体,Eclipse 强调了 Arduino 库中的方法。因此,如果我使用Serial.println("hello");,它会强调println()并声称无法解决。然后它编译得很好,并且该方法在上传到 arduino 板时工作。

EDIT2: I found my error, turns out I was trying to use some C++ functions in a C file, and eclipse didn't like it; I renamed to .cpp and all the red disappeared ;) Thanks for your help!

EDIT2:我发现了我的错误,结果我试图在 C 文件中使用一些 C++ 函数,而 eclipse 不喜欢它;我重命名为 .cpp,所有红色都消失了 ;) 感谢您的帮助!

采纳答案by Cloud

For starters, what color is the underline? This makes a difference, as yellow means it's a warning, and red means it's an error (critical, will not build in most circumstances).

首先,下划线是什么颜色?这会有所不同,因为黄色表示警告,红色表示错误(严重,在大多数情况下不会构建)。

Second, you need to look at the "Problems" tab to see if there are actual errors. If there is nothing there, then it did indeed compile correctly.

其次,您需要查看“问题”选项卡以查看是否存在实际错误。如果那里什么都没有,那么它确实编译正确。

Now, back to the original question. Depending on the type of project you are building, this type of behavior is not that uncommon. Eclipse seems to do a poor job of indexing certain projects. When you run "make all" from the command line (which is effectively what Eclipse does during build) it is likely resolving all of your code and building it just fine.

现在,回到最初的问题。根据您正在构建的项目类型,这种类型的行为并不少见。Eclipse 似乎在索引某些项目方面做得很差。当您从命令行运行“make all”(这实际上是 Eclipse 在构建期间所做的)时,它可能会解析您的所有代码并将其构建得很好。

However, Eclipse uses a different, separate toolfor indexing all of your source code and resolving variable/function definitions and declarations. This is literally a case of the left hand not knowing what the right hand is doing.

但是,Eclipse 使用不同的单独工具来索引所有源代码并解析变量/函数定义和声明。这实际上是左手不知道右手在做什么的情况。

回答by dolphy

Eclipse may or may not be pulling the paths to index from your build setup, depending on the configuration. Most likely, it is not...it's building correctly because your build setup is just fine, and you can probably build by hand.

Eclipse 可能会也可能不会从您的构建设置中提取索引路径,具体取决于配置。最有可能的是,它不是......它正在正确构建,因为您的构建设置很好,并且您可能可以手动构建。

The CDT indexer (which is the engine for deciding where all those pretty underlines, as well as code completion, F3 declaration jumping, etc comes from) isn't smart enough in a lot of cases to parse out your Makefiles and know where to look for headers and source. You need to tell Eclipse that information manually.

CDT 索引器(它是决定所有那些漂亮的下划线以及代码完成、F3 声明跳转等来自何处的引擎)在很多情况下不够聪明,无法解析您的 Makefile 并知道在哪里查找用于标题和来源。您需要手动告诉 Eclipse 该信息。

Go to Project Properties -> C/C++ General -> Paths and Symbols.

转到Project Properties -> C/C++ General -> Paths and Symbols

The amount of work you need to put into this can vary greatly, depending on your environment. If this external library is the only thing giving you headaches, then you probably just need to add the paths for that library and reindex:

您需要投入的工作量可能会有很大差异,具体取决于您的环境。如果这个外部库是唯一让您头疼的事情,那么您可能只需要添加该库的路径并重新索引:

Right-click on the project and select Index -> Rebuild

右键单击项目并选择索引 -> 重建

回答by Zormex

The solution below worked for me: Click to your project using right click. Then: Properties -> C/C++ General -> Paths and Symbols -> Symbols -> GNU C++.Almost for sure there are no symbols at all if you have this problem. Add symbol "__cplusplus" with value "201402L" After this: Right click on Project -> Index -> RebuildYou are done.

下面的解决方案对我有用:使用右键单击单击您的项目。然后:属性 -> C/C++ 常规 -> 路径和符号 -> 符号 -> GNU C++。如果您遇到此问题,几乎可以肯定根本没有符号。添加符号“__cplusplus”,值为“201402L” 之后:右键单击项目 -> 索引 -> 重建你就完成了。

回答by Compfy

When resolving symbols, CDT indexer seems to consider all header files irrespectively of which ones are actually included in the compilation unit. There is a corresponding bug report filed with Eclipse Bugzilla: https://bugs.eclipse.org/bugs/show_bug.cgi?id=439553

在解析符号时,CDT 索引器似乎会考虑所有头文件,而不管编译单元中实际包含哪些头文件。Eclipse Bugzilla 有相应的错误报告:https://bugs.eclipse.org/bugs/show_bug.cgi ?id =439553

回答by robor78

I had include folders in

我有包含文件夹

Project Properties -> C/C++ General -> Paths and Symbols -> Includes

项目属性 -> C/C++ General -> Paths and Symbols -> Includes

When I removed those, the red underlines went away, i.e. the build and the IDE where in sync.

当我删除它们时,红色下划线消失了,即构建和 IDE 同步。

回答by Petro Shm

I had the same problem. Index -> Rebuilddidn't help. When I added line #include <avr/iom1280.h>in main.cppand made Index -> Rebuildunderlines dissapeared. Then I deleted line #include <avr/iom1280.h>and project still without inderlines.

我有同样的问题。 索引 -> 重建没有帮助。当我#include <avr/iom1280.h>main.cpp 中添加 line并使Index -> Rebuild下划线消失时。然后我删除了行#include <avr/iom1280.h>和项目仍然没有中线。

Replace iom1280.hwith name of your controller. Look at the "avr\include\avr\"folder for available names

替换iom1280.h为您的控制器名称。查看“avr\include\avr\”文件夹中的可用名称

回答by E. Nusinovich

Eclipse does not work as well with C++ as it does with Java, but it should warn you about issues once you press "Rebuild" in the menu bar.

Eclipse 在 C++ 上的运行不如在 Java 上好,但是一旦您按下菜单栏中的“重建”,它就会警告您有关问题。

Try that, and see if it resolves your problem.

试试这个,看看它是否能解决你的问题。

回答by Hack06

In my case the problem was due to adding "-std=c++17" flag in the language standard field in the project properties under the compile dialect. After that the build was passing with errors, but the program was running fine. So the trick of Index>Rebuild resolved everything.

在我的情况下,问题是由于在编译方言下的项目属性的语言标准字段中添加了“-std=c++17”标志。之后构建通过错误,但程序运行良好。所以索引>重建的技巧解决了一切。

回答by MathewKarthick

If you changed something in the configurations, (example, editing in *.cproject file with notepad++) , the below options helps. Build Configurations--> Clean Alland then Index--> Rebuild

如果您更改了配置中的某些内容(例如,使用 notepad++ 在 *.cproject 文件中编辑),以下选项会有所帮助。 构建配置-->清除所有然后索引-->重建