The only time you have to include a header in another .h file is if you need to access the type definition in that header; eg:
#ifndef MY_HEADER_H #define MY_HEADER_H #include <stdio.h> void doStuffWith(FILE *f); // need the definition of FILE from stdio.h #endif
If heading A depends on heading B, such as the example above, then heading A should include heading B directly. Do NOT try to order your included in .c file to satisfy the dependencies (that is, including the B header before the A header); itโs a big antiquity of heartburn, awaiting its ministry. I'm serious. I have been to this film several times, and it always ends in Tokyo on fire.
Yes, this can lead to the fact that the files will be included several times, but if they have the proper guards enabled, configured to protect against several declaration / definition errors, then a few extra seconds of build time should not be worried. Trying to manage dependencies manually is a pain in the ass.
Of course, you should not include files that you do not need.
John Bode Jun 09 2018-10-06T00: 00Z
source share