Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#include inside function body doesn't work (CDT/Eclipse C++)

This question concerns a C++ project managed with CDT 8.1.2 inside Eclipse 4.2.2 (Juno). The following code snippet will compile but it will be displayed as having errors inside Eclipse.

I have a file named foo.h which reads:

int a = 42;

This file is included in another file foo.cpp:

#include <cstdio>

int main() {
    #include "foo.h"
    printf("%d", a);
    return 0;
}

How can I fix the "Symbol 'a' could not be resolved"? In my understanding, the #include statement inside the main() function is supposed to trigger a mere copy-paste action in the preprocessor. CDT seems to have indexed the file correctly because I can CTRL-Click on the filename "foo.h" which then opens the file in the IDE. Interestingly, if I move the #include "foo.h" statement just below the #include <cstdio> statement, it works as expected. Is there any option inside CDT to perform preprocessing before resolving symbols?
Side note: I know that this code design is frowned upon, however I need to import code written by somebody else and require content assist set up properly in order to be productive.

like image 556
Johannes P Avatar asked Jun 08 '13 22:06

Johannes P


2 Answers

Looks like there are many known issues with the code analyser in Eclipse. See for example this question Turn off eclipse errors (that arent really errors) on how to disable some or all of the error messages. Not sure if that "solution" will be good enough for you but from what I understand it should not impact other aspects of the code indexer.

like image 158
Bishop Avatar answered Oct 26 '22 22:10

Bishop


Since any include should just turn out to be some "plain copy&paste" this may well be a bug in Eclipse/CDT.

If you use KDE you may try KDevelop.

I don't know whether there is everything around what you need but I just checked and there is at least no confusion about your minimal example above. (Code highlightning and assist is working as far as I can tell.)

enter image description here

Another quite popular IDE for Linux is Netbeans. I can't give you any information on whether it will support that sort of "inline including" but both of them are popular and worth a try.

Perhaps this error is a "hint" for you to refrain from doing what your intention was. I don't know whoever would write code into a header that is mean to be included inline but instead of putting effort in a change of your IDE / search for a new one, you should probably refactor the code, There would be a greater benefit I think.

like image 24
Pixelchemist Avatar answered Oct 27 '22 00:10

Pixelchemist