2

I'm trying to learn how to use Eclipse for C++ development (using MinGW) and have run into an interesting problem.


While writing a simple test program I get the following error:

enter image description here

However simply saving the file resolves the error... Why does this happen?

After saving:

enter image description here


I would really like simple bugs like this to be caught without having to go and manually save the file...

I know it's really simple to just click "Save" but I know myself and I will forget and I will spend hours trying to track down a bug that isn't actually a bug. (I'm sure this will probably also happen with things other than using namespace std;.)

tjwrona1992
  • 8,614
  • 8
  • 35
  • 98
  • `However simply saving the file resolves the error... Why does this happen?` Maybe the Eclipse plugin has a bug? – PaulMcKenzie Dec 01 '15 at 18:08
  • 3
    Use this as a lesson to never, never, never again type those dreaded words `using namespace std;`. Never again in your life! – SergeyA Dec 01 '15 at 18:14
  • But i just wanted to say hello to the world :( – tjwrona1992 Dec 01 '15 at 18:20
  • 1
    @SergeyA: Writing out `std::cout << "Hello" << std::endl;` will also act the same way. `std::cout` and `std::endl` get underlined red until i click "save" – tjwrona1992 Dec 01 '15 at 18:32
  • While it doesn't solve the problem, using the keyboard shortcut would likely lessen the annoyance of this. The exact shortcut depends on the configuration, which comes with multiple defaults and which [you can modify according to your preferences](http://stackoverflow.com/questions/461743/is-there-anyway-to-modify-the-keyboard-shortcuts-in-eclipse) – jaggedSpire Dec 01 '15 at 18:43
  • @jaggedSpire: Is it typical for eclipse to require the file to be saved to see all of the dependencies? – tjwrona1992 Dec 01 '15 at 19:21
  • I wouldn't call that a bug. Eclipse's CODAN tool isn't exactly a gazelle, so as-you-type parsing is probably a system killer. This may improve with time. In the meantime, save regularly. – user4581301 Dec 01 '15 at 19:26
  • Expanding on that last thought to try to answer @tjwrona1992 's last comment. Yes. Eclipse's CODAN tool runs as you type, (Project->Properties->C/C++ General->Code Analysis->Launching), but it only parses dependencies on demand, usually with a save. You will also find that if you are editing included headers in another project, you will have to force an index rebuild (Project->C/C++ Index->Rebuild) to catch the modifications. – user4581301 Dec 01 '15 at 19:42
  • Thanks @user4581301, that all makes sense. You should have just posted that as an answer :) – tjwrona1992 Dec 01 '15 at 20:40

1 Answers1

4

Eclipse's CODAN tool runs as you type, unfortunately it only parses dependencies on demand, usually with a save.

Why? Eclipse's CODAN tool isn't exactly a gazelle, so having to track through all of the file's dependencies while the user is typing is probably a system killer. This may improve with time. In the meantime, save regularly.

And, to be honest, this is probably a dodge. It should only have to search dependencies when a dependency is added. But there are a lot of buts. What about when a dependency is added to a dependency? Or a new dependency is hidden in a macro (don't do this) and hard to parse without digging through the dependencies? Or exposed by adding a define that triggers conditional compilation (I prefer different implementation files and letting the linker sort it out to conditional compilation)? What about...?

Blah. If people want to write garbage code, that's their problem. A static analyzer should focus on the needs of people who aren't trying to trick it and circumvent good style. That said, I don't know the CODAN code, and I don't know how deep one would have to go to change it to catch and handle the easy cases at a tolerable real-time rate.

But at the end of the day, the only analyzer you should pay attention to is the compiler--with the warning levels turned up to 11, of course. CODAN is not perfect. It misses and misinterprets stuff, and you may find yourself hunting a bug that's not a bug in your code. If the compiler has a bug, that's a different case, but a lot less likely. Definitely take CODAN's help, but before you spend time on an odd error, make sure it really is an error by saving and building the program.

CODAN configuring stuff:

Most of CODAN's options can be found by visiting Project->Properties on the menu and navigate the Properties dialogue to C/C++ General->Code Analysis

To turn configure CODAN's run options, to turn off updating as you type for example, go one step further to C/C++ General->Code Analysis->Launching

You will also find that if you are editing included headers in another project, you will have to force an index rebuild to to catch the modifications. Select Project->C/C++ Index->Rebuild from the menu for the project doing the including.

user4581301
  • 33,082
  • 7
  • 33
  • 54