Is it a good idea to put all of your includes in one header file?

后端 未结 7 837
青春惊慌失措
青春惊慌失措 2021-01-04 21:08

What is the best practice for C what you put into a C header file?

Is it useful to put all the includes used for a program across multiple source files in one header

7条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-04 21:32

    I personally subscribe to a "put it where you use it" philosophy. It makes it much clearer which files use what, where dependencies are, etc.

    Imagine you have a header MyHeader.h. If you make a change in it that requires changing code that relies on it, it's easy to find that dependent code if each file that uses it has a #include "MyHeader.h" - you can just do a global search for the include statement.

    If on the other hand you only include MyHeader.h in some other header MyHugeHeader.h, and then include that in your files you can't do the same, since all that's in the files that use MyHeader.h is #include "MyHugeHeader.h", same as every other file.

提交回复
热议问题