Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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. Func2.C

  4. Func3.C

where main() is in Main.C file. Should there be four header files

  1. Main.h

  2. Func1.h

  3. Func2.h

  4. Func3.h

Or there should be only one header file for all .C files?

What is a better approach?

like image 217
Vicky Avatar asked Mar 03 '09 23:03

Vicky


People also ask

Should every C file have a header file?

If your file exposes an interface - that is, if it has functions which will be called from other files - then it should have a header file. Otherwise, it shouldn't.

Should header files include other header files?

Every header file A.h should #include every other header file that A.h requires to compile correctly, but no more. What is needed in A.h: If another structure type X is used as a member variable of a structure type A, then you must #include X.h in A.h so that the compiler knows how large the X member is.

Why are header files needed in every C program?

Header files are needed to declare functions and variables that are available. You might not have access to the definitions (=the . c files) at all; C supports binary-only distribution of code in libraries.

Can we write C program without header files?

Yes it is possible to write a simple program without header files, but why would you do that ? Header files are useful to share definitions, constants, functions prototypes, etc between multiple files or modules.


2 Answers

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.

like image 196
paxdiablo Avatar answered Nov 22 '22 05:11

paxdiablo


  1. Header files are not mandatory.

  2. #include simply copy/paste whatever file included (including .c source files)

  3. Commonly used in real life projects are global header files like config.h and constants.h that contains commonly used information such as compile-time flags and project wide constants.

  4. A good design of a library API would be to expose an official interface with one set of header files and use an internal set of header files for implementation with all the details. This adds a nice extra layer of abstraction to a C library without adding unnecessary bloat.

  5. Use common sense. C/C++ is not really for the ones without it.

like image 36
Anders Hansson Avatar answered Nov 22 '22 05:11

Anders Hansson