简体   繁体   中英

How to determine the appropriate identifier of conditional inclusion (#ifndef) in header files for C++?

I was using an IDE that gave me the following line when I created a new HeaderFile.h

#ifndef CODE_HEADERFILE_H

but I've also seen usages such as:

#ifndef _HeaderFile_
#ifndef HEADERFILE_H

is the label completely arbitrary given that it will be called if the header file is loaded multiple times? And what would happen if another different header file had the same identifier and both where included somewhere?

Include guard define naming is personal, but conventionally it's in the form of

HEADER_FILE_NAME_H

But you can pick whatever name you'd like as long as it doesnt clash with other include guards otherwise only 1 file will end up getting included.

There's also #pragma once , although it's not supported by the standard, all major compilers support it. This eliminates the need for include guards all together.

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:

  1. 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.

  2. 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.

  3. 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.

maybe, It all depends on you and your company or your group's code style. When I write #ifdef or #ifndef, usually, I write the statement like that below: 1) #ifndef HEADERFILE_H 2) #ifndef HEADER_FILE_H 3) #ifndef PATH_TO_YOUR_HEADERFILE_HEADERFILENAME_H

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM