How to structure #includes in C

后端 未结 6 939
眼角桃花
眼角桃花 2020-12-06 07:48

Say I have a C program which is broken to a set of *.c and *.h files. If code from one file uses functions from another file, where should I include the header file? Inside

6条回答
  •  天涯浪人
    2020-12-06 08:15

    I include the most minimal set of headers possible in the .h file, and include the rest in the .c file. This has the benefit of sometimes reducing compilation times. Given your example, if foo.h doesn't really need bar.h but includes it anyway, and some other file includes foo.h, then that file will be recompiled if bar.h changes, even though it may not actually need or use bar.h.

提交回复
热议问题