#include inside function body does not work (CDT / Eclipse C ++)

This question is about a C ++ project managed by CDT 8.1.2 inside Eclipse 4.2.2 (Juno). The following code fragment will be compiled, but it will appear as having errors inside Eclipse.

I have a file called foo.h that says:

int a = 42; 

This file is included in another foo.cpp file:

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

How can I fix the "A" character cannot be resolved? In my understanding, the #include statement inside the main () function should trigger a simple copy-paste action in the preprocessor. CDT seems to index the file correctly, because I can use CTRL- click on the name of the file "foo.h" which then opens the file in the IDE. Interestingly, if I move the #include "foo.h" statement just below the #include <cstdio> , it works as expected. Is there Is there any option inside CDT to do preprocessing before character resolution?
Side note. I know this code design was frowned upon, however I need to import the code written by someone else and require the content to be configured correctly in order to be productive.

+6
source share
7 answers

There seem to be many known issues with the Eclipse code analyzer. See For example, this question Disable eclipse errors (which really are errors) on how to disable some or all error messages. Not sure if this “solution” will be enough for you, but from what I understand it should not affect other aspects of the code indexer.

+2
source

Since any inclusion should be just a “simple copy and paste,” this could be a bug in Eclipse / CDT.

If you use KDE, you can try KDevelop .

I don't know if there is everything around that you need, but I just checked and at least there is no confusion in your minimal example above. (Code naming and help work as far as I can tell.)

enter image description here

Another pretty popular IDE for Linux is Netbeans . I can not give you any information on whether it will support such a "built-in", but both of them are popular and deserve attention.

Perhaps this mistake is a "clue" that you refrain from what was your intention. I don’t know whoever writes the code in the header, which should be included in the string, but instead of putting effort into changing your IDE / searching for a new one, you probably should reorganize the code. It will be more useful I think.

+2
source

Typically, C ++ includes files (.h or header files) that should be included at the head of the module, and not mixed with the module code.

It seems that you have encountered a bug in your development environment that cannot correctly handle the header file included in the middle of the function.

Move your #include "foo.h" status above your void main()... statement void main()... and try again.

0
source

Your latest update implies that you can use other IDEs. Do they need to support GCC or are you limited to working on Linux or just use CDT to provide graphical display of calls, etc.

Can you use something like Visual Studio express to get a compilation of the project and use the tools available there to understand how the code is structured?

It is unclear whether you can really use a different IDE just for understanding the code or if you need to provide it using an alternative IDE.

VS express 2012 works great with this style.

0
source

Some of my colleagues use Source Insight (a commercial but fair price) and are pretty convinced of that. But, unfortunately, this is only available for Windows. It seems.

In addition to Code Blocks (as mentioned earlier), you can try the CodeLite IDE , it can not say anything about its quality compared to Eclipse CDT.

0
source

Type extern int a; ...............

-1
source

How about forward-declaring int a; ?

-1
source

Source: https://habr.com/ru/post/946870/


All Articles