How to properly name include guards in c++

后端 未结 4 1511
忘掉有多难
忘掉有多难 2021-01-21 08:05

I have 3 header files:

misc.h
MyForm.h
Registration.h

In misc.h file I put

#ifndef MISC_H
#define MISC_H
#endif
相关标签:
4条回答
  • 2021-01-21 08:38

    How to name include guards is somewhat opinion based, but a few recommendations can be made:

    1) Don't make them short as they are then likely to clash with other macros defined in other files or the implementation or libraries you use.

    2) Make them contain a project specific prefix in an attempt to make them unique and unambiguous.

    3) If your project contains multiple sub-parts "modules"/"libraries" etc, make the guard name contain the name of the module to make them unique within your project - include the file name as well.

    4) Make sure not to use reserved names such as NULL, names starting with a number or names that begin with an underscore followed by an uppercase letter as well as names containing consecutive underscores; all such names are reserved for the implementation to use.

    5) Use all uppercase for macros (just a common convention).

    For a file named "baz.h" in a project named "foo", in submodule "bar", I'd name the guard FOO_BAR_BAZ_H.

    PS. Make sure all your headers use include guards (except in very special and rare cases). Otherwise you are inviting trouble with multiple definition errors and more.

    0 讨论(0)
  • 2021-01-21 08:38

    You should use include guards in every header file and nothing should be outside the include guards.

    The name of include guards can be anything as long as it is unlikely to clash with any other name. An example for your MyForm.h might be:

    #ifndef MYFORM_HEADER_
    #define MYFORM_HEADER_
    
    #include "misc.h"
    #include "Registration.h"
    
    /* all code here */
    
    #endif
    

    It is also important that nothing is written outside of the include guards in a header file.

    0 讨论(0)
  • 2021-01-21 08:39

    How to properly name include guards in c++

    Usual convention is to only use upper case characters. Numbers and underscores are fine too except not as the first character. The include guard must be unique: The name must not be used for anything else in the program.

    The name must not be reserved, so you must not use NULL for example. Also, all names that begin with underscore followed by a capital letter, as well as all names that contain two consecutive underscores are reserved.

    MISC_H is not reserved, but keep in mind that if you have another misc.h in another path, then you cannot use the same include guard. It's usually better to use longer names to uniquely identify the file. Perhaps use some kind of prefix, such as company name, or your own name. Typically _H suffix is used to separate from non-guard macros.

    If you have problems with coming up unique names that aren't reserved, or just feel that it's tedious, then you could use #pragma once instead. Be warned that it isn't standard. I've heard rumours about exotic compilers that don't support it, as well as convoluted build processes where hard linked files didn't get guarded as they should. If you encounter such problems, it's trivial to simply add the header guards afterwards.


    Your other headers appear to lack include guards. If you include them in any header, then there will probably be trouble. It's a good practice to guard all header files.


    do I need to put #endif at the bottom of misc.h?

    Yes. The guard only protects the code that is between the define and endif.

    0 讨论(0)
  • 2021-01-21 09:02

    You need to put individual include guards in every header, notably in MyForm.h:

    #ifndef MYFORM_H
    #define MYFORM_H
    #include "misc.h"
    #include "Registration.h"
    // etc...
    #endif /*MYFORM_H*/
    

    and in Registration.h:

    #ifndef REGISTRATION_H
    #define REGISTRATION_H
    // actual code
    // etc...
    #endif /*REGISTRATION_H*/
    
    0 讨论(0)
提交回复
热议问题