How to determine the appropriate identifier of conditional inclusion (#ifdef) in header files for C++?
In the exactly same way as you would determine the name of any variable, macro, function or a type:
Firstly, consult the language rules on what identifiers are reserved to the language implementation, and do not use any of those identifiers.
#ifndef _HeaderFile_
This is an example of a reserved identifier. Do not define it unless you are implementing the standard library.
Next, rule out any identifier used for any other purpose in the program. If you have variable named x
, then do not use that as a header guard. If you have one header guard named y
, then do not use that as another header guard. If you have a function named XYZ_H
, then do not use that as a header guard.
Now, choose any identifier from the remaining names. There should be plenty to choose from.
is the label completely arbitrary
Only as much as any other variable, macro, function or type name is arbitrary. You simply need to come up with a unique name.
You don't however need to refer to the header guard macro in any other context, so you don't need to worry about it being meaningful unlike most other names in the program.
And what would happen if another different header file had the same identifier and both where included somewhere?
What happens to ifndef FOO
when FOO
is defined? The pre-processor will remove everything after the directive until the next #endif
directive (or #else
, but that is rare with header guards). This is how header guards prevent multiple inclusions into same file: Subsequent inclusions are made empty by the pre-processor directive. In case multiple headers share the same guard, the one that is included second will be empty. This will probably be a problem.