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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With