Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

#include file : Circular reference

Status
Not open for further replies.

eblc1388

Active Member
What will a compiler do if the include file includes another file which includes the original file?

Edited 24hr later: the recursive loading will not occur as I failed to spot the scope of the #ifndef directive, which extends to end of file.
 

Attachments

  • messed_up.gif
    messed_up.gif
    58.4 KB · Views: 199
Last edited:
Does it have issues compiling? Having files that do that isn't the best coding style, but all the ifndef/define in the front of the header prevents infinite loops from occuring, and at that point its more a matter of what's actually defined in each file.
 
hjames said:
Does it have issues compiling? Having files that do that isn't the best coding style, but all the ifndef/define in the front of the header prevents infinite loops from occuring, and at that point its more a matter of what's actually defined in each file.

If there is conditional statement to decide whether to load the file then it will not be an issue anymore. Problem is that both files do not contains any such construct to prevent such looping. If you study the image you'll see.

It does not appear to have issues to the compiler but that could be because the "#include" directive would only expands to a certain level deep say 8 or 16 so I think it just keeps on loading the same file into the memory as instructed.

Really bad indeed.
 
There is conditional to check for recursion - the #ifndef, #define "sentinel" at the front of the header file causes the a include file to be opened - and if it's already been included the entire header file gets truncated by the preprocessor.
 
hjames said:
There is conditional to check for recursion - the #ifndef, #define "sentinel" at the front of the header file causes the a include file to be opened - and if it's already been included the entire header file gets truncated by the preprocessor.

Yes, you have hit the nail spot on.

Owing to my limited experience towards header files I failed to realised that the remaining body of the file that follows the "#ifndef" directive is actually inside a conditional, with the scope extends to the #endif at the very end, so the recursive looping will not occur as I thought it would.
 
Last edited:
It's just one of those things that they just teach you to type in without fully explaining things. Most compiler should have some "preprocess only" option that will halt the compiler immediately after running the preprocessor. It's a pretty educational experience tracing through the include files and seeing what the compiler actually sees.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top