Should every C or C++ file have an associated header file?

后端 未结 15 2245
情话喂你
情话喂你 2021-02-04 06:30

Should every .C or .cpp file should have a header (.h) file for it?

Suppose there are following C files :

  1. Main.C

  2. Func1.C

  3. <
相关标签:
15条回答
  • 2021-02-04 07:07

    For a start, it would be unusual to have a main.h since there's usually nothing that needs to be exposed to the other compilation units at compile time. The main() function itself needs to be exposed for the linker or start-up code but they don't use header files.

    You can have either one header file per C file or, more likely in my opinion, a header file for a related group of C files.

    One example of that is if you have a BTree implementation and you've put add, delete, search and so on in their own C files to minimise recompilation when the code changes.

    It doesn't really make sense in that case to have separate header files for each C file, as the header is the API. In other words, it's the view of the library as seen by the user. People who use your code generally care very little about how you've structured your source code, they just want to be able to write as little code as possible to use it.

    Forcing them to include multiple distinct header files just so they can create, insert into, delete from, and search, a tree, is likely to have them questioning your sanity :-)

    You would be better off with one btree.h file and a single btree.lib file containing all of the BTree object files that were built from the individual C files.


    Another example can be found in the standard C headers.

    We don't know for certain whether there are multiple C files for all the stdio.h functions (that's how I'd do it but it's not the only way) but, even if there were, they're treated as a unit in terms of the API.

    You don't have to include stdio_printf.h, stdio_fgets.h and so on - there's a single stdio.h for the standard I/O part of the C runtime library.

    0 讨论(0)
  • 2021-02-04 07:08

    If you want your compiled code to be used from another compilation unit you will need the header files. There are some situations for which you do now need/want to have a headers.

    The first such case are main.c/cpp files. This class is not meant to be included and as such there is no need for a header file.

    In some cases you can have a header file that defines behavior of a set of different implementations that are loaded through a dll that is loaded at runtime. There will be different set of .c/.cpp files that implement variations of the same header. This can be common in plugin systems.

    0 讨论(0)
  • 2021-02-04 07:11

    Generally it's best to have a header file for each .c file, containing the declarations for functions etc in the .c file that you want to expose. That way, another .c file can include the .h file for the functions it needs, and won't need to be recompiled if a header file it didn't include got changed.

    0 讨论(0)
  • 2021-02-04 07:13

    It depends. Usually your reason for having separate .c files will dictate whether you need separate .h files.

    0 讨论(0)
  • 2021-02-04 07:13

    Generally cpp/c files are for implementation and h/hpp (hpp are not used often) files are for header files (prototypes and declarations only). Cpp files don't always have to have a header file associated with it but it usually does as the header file acts like a bridge between cpp files so each cpp file can use code from another cpp file.

    One thing that should be strongly enforced is the no use of code within a header file! There's been too many times where header files break compiles in any size project because of redefinitions. And that's simply when you include the header file in 2 different cpp files. Header files should always be designed to be included multiple times as well. Cpp files should never be included.

    0 讨论(0)
  • 2021-02-04 07:16

    As already noted, generally, there will be one header (.h) file for each source (.c or .cpp) file.

    However, you should look at the cohesiveness of the files. If the various source files provide separate, individually reusable sets of functions - an ideal organization - then you should certainly have one header per file. If, however, the three source files provide a composite set of functions (that is too big to fit into one file), then you would use a more complex organization. There would be one header for the external services used by the main program - and that would be used by other programs needing the same services. There would also be a second header used by the cooperating source files that provides 'internal' definitions shared by those files.

    (Also noted by Pax): The main program does not normally need its own header - no other source code should be using the services it provides; it uses the services provided by other files.

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