Header per source file

后端 未结 7 1652
醉梦人生
醉梦人生 2020-11-29 05:53

I\'m trying to understand the purpose behind one header per each source file method. As I see it, headers are meant for sharing function declarations, typedef\'

相关标签:
7条回答
  • 2020-11-29 06:04

    You only put in your header file the bare minimum that other source files need to "see" in order to compile. I've seen some people that put everything non-code into the header file (all typedefs, all #define's, all structures, etc.) even if nothing else in the codebase will be using those. That makes the header file much harder to read for yourself and those who want to use your module.

    0 讨论(0)
  • 2020-11-29 06:05

    Because, as you said yourself, it is not feasible to put the "whole software" into one source file.

    If your program is very small, then yes it's is simpler just to put everything in one .c file. As your program gets larger, it becomes helpful to organize things by putting related functions together in different .c files. Further, in the .h files you can restrict the declarations you give to declarations of things that are supposed to be used by things in other .c files. If a .c file doesn't contain anything that should be accessible outside itself, it needs no header.

    For example, if .c has function foo() and fooHelper(), but nobody except foo() is supposed to call fooHelper() directly, then by putting foo() and fooHelper() into foo.c, only putting the declaration of foo() in foo.h, and declaring fooHelper() as static, it helps to enforce that other parts of your program should only access foo() and should not know or care about fooHelper(). Kind of a non object-oriented form of encapsulation.

    Finally, make engines are generally smart enough to rebuild only those files which have changed since the last build, so splitting into multiple .c files (using .h files to share what needs to be shared) helps speed up builds.

    0 讨论(0)
  • 2020-11-29 06:05

    Generally a header for a source file method means that you declare only the functions from that compilation unit in that header.

    That way you don't pollute with declarations you don't need. (in large software project might be a problem)

    As for separate compilation units, these speed up the compilation and can help you avoid collisions if private symbols are declared static.

    0 讨论(0)
  • 2020-11-29 06:09

    You don't need one header per source file. One header per module, containing the public interface, and maybe an additional header containing private declarations etc shared between files in that module.

    0 讨论(0)
  • 2020-11-29 06:12

    The header files in C separate declarations (which must be available to each .c file that uses the functions) from the definitions (which must be in one place). Further, they provide a little modularity, since you can put only the public interface into a header file, and not mention functions and static variables that should be internal to the .c file. That uses the file system to provide a public interface and private implementation.

    The practice of one .h file to one .c file is mostly convenience. That way, you know that the declarations are in the .h file, and the definitions in the corresponding .c file.

    0 讨论(0)
  • 2020-11-29 06:14

    Programmers use this method because it allows them to separate interface from implementation while guaranteeing that client code and implementation agree on the declarations of the functions. The .h file is the "single point of truth" (see Don't Repeat Yourself) about the prototype of each function.

    (Client code is the code that #include's the .h file in order to use the exported functions, but does not implement any of the functions in the .h.)

    0 讨论(0)
提交回复
热议问题