Should I, or shouldn't, include the same headers in different c files, which, in turn, are the headers used in the main file?

I am creating a main.c file to use functions from several different .h files. Some of these .h files (or rather, their .c source files) use the same thing (standard, but also some others)

My question is: is everything all right if I just include them once for all the header files in my main.c, or should I allow each .h file to include them separately and not to include in my main.c (given that I only use functions from these header files)?

Or should I do both?

How am I doing it now:

dist.c: #include "dist.h" #include <stdio.h> #include <unistd.h> #include "rpiGpio.h" #include <pthread.h> #include <wiringPi.h> #include <softPwm.h> 

Then for another:

 cmps.c: #include "cmps.h" #include <stdint.h> #include <stdio.h> #include <unistd.h> #include <math.h> #include "rpiGpio.h" 

Then in my main.c:

 #include <stdio.h> #include <stdlib.h> #include "dist.h" #include "cmps.h" 

Thanks in advance!

+6
source share
2 answers

You must include the standard headers above your own headers, and you must include all the dependencies for the file in this file. If you modify incoming files in one file, this should not affect other files. Each file should contain its own list of header dependencies.

If in your example dist.h includes <stdio.h> , you should not rely on this outside dist.h If you change dist.h so that it no longer depends on <stdio.h> and removes this #include , then your program will abort.

+6
source

I think the answer is "it depends." As long as you agree, I think that everything is in order. Some advantages and disadvantages of different approaches:

  • Inclusion twice in the system header does not cause side effects (due to the protection of the headers); including your own headers twice should also not cause problems if you also use header protectors. However, perhaps this may slow down compilation.

  • In some cases, especially in older Unices, the order in which headers are included matters (unfortunately). Sometimes the order of #define (for example, #define _GNU_SOURCE ) matters in relation to #include . I also had this happen with Linux include files for various internal bits of the network (I now forget that). For this reason, it is recommended that you always turn on your system, including (and #define , which they consider) in a consistent manner.

  • One way to do this is to include your entire system in one own native include file and include them from each .c file; this gives the same result as with autoconf and the generated config.h . However, it may unnecessarily include files that slow down compilation.

  • Some say that includes system headers over your own included ones. Although this is often seen as good practice, it does not work perfectly if your own reference .h file types defined on the system include, for example, stdint.h defines int32_t . If you included this only in your .h file, you will return to the potential problem with including the order and whether you will introduce #define _GNU_SOURCE in the right place.

  • Therefore, my personal coding style should have the equivalent of config.h , which is included by all .h files and (for a good grade) by all .c files as the first include that contains all the relevant system includes. This is not an ideal compilation time, but what are the purpose of precompiled headers. I usually place the system in alphabetical order if there is no reason for this.

  • An alternative is to (carefully) do this by file, as @meagar suggests.

+3
source

Source: https://habr.com/ru/post/980499/


All Articles