Should I use #include in headers?

后端 未结 9 1318
甜味超标
甜味超标 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条回答
  •  Happy的楠姐
    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 
    
    struct S
    {
        gchar c;
    };
    
    #endif /* MY_HEADER_H */
    

    and

    /* myCode.c */
    #include "myHeader.h"
    
    void myFunction()
    {
        struct S s;
        /* really exciting code goes here */
    }
    

提交回复
热议问题