Should I use #include in headers?

后端 未结 9 1188
甜味超标
甜味超标 2020-11-22 04:55

Is it necessary to #include some file, if inside a header (*.h), types defined in this file are used?

For instance, if I use GLib and wish to use the

相关标签:
9条回答
  • 2020-11-22 04:56

    I use the following construct to be sure, that the needed include file is included before this include. I include all header files in source files only.

    #ifndef INCLUDE_FILE_H
     #error "#include INCLUDE.h" must appear in source files before "#include THISFILE.h"
    #endif
    
    0 讨论(0)
  • 2020-11-22 04:56

    What I normally do is make a single include file that includes all necessary dependencies in the right order. So I might have:

    #ifndef _PROJECT_H_
    #define _PROJECT_H_
    #include <someDependency.h>
    #include "my_project_types.h"
    #include "my_project_implementation_prototypes.h"
    #endif
    

    All in project.h. Now project.h can be included anywhere with no order requirements but I still have the luxury of having my dependencies, types, and API function prototypes in different headers.

    0 讨论(0)
  • 2020-11-22 05:00

    During compilation preprocessor just replaces #include directive by specified file content. To prevent endless loop it should use

    #ifndef SOMEIDENTIFIER
    #define SOMEIDENTIFIER
    ....header file body........
    #endif
    

    If some header was included into another header which was included to your file than it is not necessary to explicitly include it again, because it will be included into the file recursively

    0 讨论(0)
  • 2020-11-22 05:01

    Usually, library developers protect their includes from multiple including with the #ifndef /#define / #endif "trick" so you don't have to do it.

    Of course, you should check... but anyways the compiler will tell you at some point ;-) It is anyhow a good practice to check for multiple inclusions since it slows down the compilation cycle.

    0 讨论(0)
  • 2020-11-22 05:05

    You need to include the header from your header, and there's no need to include it in the .c. Includes should go after the #define so they are not unnecessarily included multiple times. For example:

    /* myHeader.h */
    #ifndef MY_HEADER_H
    #define MY_HEADER_H
    
    #include <glib.h>
    
    struct S
    {
        gchar c;
    };
    
    #endif /* MY_HEADER_H */
    

    and

    /* myCode.c */
    #include "myHeader.h"
    
    void myFunction()
    {
        struct S s;
        /* really exciting code goes here */
    }
    
    0 讨论(0)
  • 2020-11-22 05:13

    Yes it is necessary or the compiler will complain when it tries to compile code that it is not "aware" of. Think of #include's are a hint/nudge/elbow to the compiler to tell it to pick up the declarations, structures etc in order for a successful compile. The #ifdef/#endif header trick as pointed out by jldupont, is to speed up compilation of code.

    It is used in instances where you have a C++ compiler and compiling plain C code as shown here Here is an example of the trick:

    #ifndef __MY_HEADER_H__
    #define __MY_HEADER_H__
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    
    /* C code here such as structures, declarations etc. */
    
    #ifdef __cplusplus
    }
    #endif
    
    #endif /* __MY_HEADER_H__ */
    

    Now, if this was included multiple times, the compiler will only include it once since the symbol __MY_HEADER_H__ is defined once, which speeds up compilation times. Notice the symbol cplusplus in the above example, that is the normal standard way of coping with C++ compiling if you have a C code lying around.

    I have included the above to show this (despite not really relevant to the poster's original question). Hope this helps, Best regards, Tom.

    PS: Sorry for letting anyone downvote this as I thought it would be useful tidbit for newcomers to C/C++. Leave a comment/criticisms etc as they are most welcome.

    0 讨论(0)
提交回复
热议问题