0

I understand that the #define function is a pre-processor command that basically copies and pastes chunks of code for the lack of a better explanation. What I am not clear about is in the templates for Eclipse header files it inserts:

#ifndef <FileName>_H #define <FileName>_H //Code, definitions etc.... #endif 

I understand that it is defining a variable to tell something that the file was defined, what I don't know is why? Is this sort of the same thing in PHP as #INCLUDE_ONCE? I tried to search for this on here and Google but I am not quite sure what the nomenclature is for what I want, everything that I found explains what it is, how it works etc...

EDIT

Since this is similar to a #include once in PHP, what is the best practice for putting #include statements? I would assume that you would want them inside the #define so they didn't get included if the file was already included is this also correct? If the included files were outside of the #define command wouldn't they be included regardless?

4
  • The nomenclature is Include guard Commented May 18, 2016 at 19:40
  • First to learn is to use the word "function" very carefully. It has a specific meaning and the preprocessor commands are verry far away from it. Commented May 18, 2016 at 19:40
  • many modern compilers support #pragma once which does the same thing, and is generally easier to read. Its not standard (yet), but is fairly widely supported. Commented May 18, 2016 at 19:42
  • You can change the code file templates in Eclipse if you wish. Window -> Preferences -> C/C++ -> Code Style -> Code Templates Commented May 18, 2016 at 19:52

1 Answer 1

0

Yes, this is very similar to PHP #INCLUDE_ONCE. The idea is that a header files contains declarations for functions and variables that are used in other modules. Those modules must #include the header file to be able to access those functions and variables. But if a source file #include's a header and then #include's another header that also #include's that header, you get two copies of those declarations, which causes all sorts of problems.

This common technique prevents duplicate inclusion. It insures that any code unit that #include's a header gets a copy of it the first time it is requested, but not again.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the explanation, that's what I thought but wasn't sure. This brings up another question about this, should you put your #include's in the statement or outside? I would assume that you would want everything in the file inside the statement but I could be wrong.
That is the standard approach yes: put the include guards on the absolute outside of the file with everything else inside. But in practice, it probably doesn't matter much. If you have include guards in all your header files, then including a header twice will try to include all its dependencies twice, which will do nothing. Although that is probably slightly less efficient.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.