Should I use #include in headers?

后端 未结 9 1191
甜味超标
甜味超标 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 05:21

    Just include all external headers in one common header file in your project, e.g. global.h and include it in all your c files:

    It can look like this:

    #ifndef GLOBAL_GUARD
    #define GLOBAL_GUARD
    
    #include 
    /*...*/
    typedef int  YOUR_INT_TYPE;
    typedef char YOUR_CHAR_TYPE;
    /*...*/
    #endif
    

    This file uses include guard to avoid multiple inclusions, illegal multiple definitions, etc.

提交回复
热议问题