Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should reliance on indirect header inclusion be used?

Tags:

c

include

If a project has a header that includes a second header and both of them include a common header, is it good practice to drop the inclusion of the common header from the first header and rely on the indirect inclusion via the second header?

e.g.: I know the stdint.h can be removed from temperature.h, but should it be?

In temperature.h:

#include <stdint.h>  // *Should* this include be removed in this case.
#include "i2c.h"

extern uint16_t temperatureRead (i2cData_t x); 

In i2c.h:

#include <stdint.h>

typedef struct i2cData_t {
    uint16_t exampleMember
} i2cData_t;
like image 636
Toby Avatar asked Dec 20 '22 15:12

Toby


1 Answers

Generally you want your modules to be self contained. If your module relies on something in a header then include it. Temperature.h may one day decide it doesn't need to include stdint.h any more and have it removed. Your code should have nothing to do with that decision, so safeguard it by including stdint.h, i.e. be self contained.

Use header guards (or C++ pragma once) to make sure your compilation speed doesn't degrade due to including a header multiple times.

like image 96
Phillip Ngan Avatar answered Dec 24 '22 02:12

Phillip Ngan