Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Header per source file

Tags:

c

header

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's and macro's between several files that utilize them. When you make a header file for your .c file it has the disadvantage that each time you want to see a function declaration or macro you need to refer to the header file, and generally it is simpler that everything is in one source file (not the whole software, of course).

So why do programmers use this method?

like image 878
Ori Popowski Avatar asked Jul 22 '09 20:07

Ori Popowski


People also ask

What is source header file?

A header file is a file containing C declarations and macro definitions (see Macros) to be shared between several source files. You request the use of a header file in your program by including it, with the C preprocessing directive ' #include '.

Should a source file include its own header?

The header file tells people what the source file can do. So the source file for the header file needs to know its obligations. That is why it is included.

Is a header file a source code file?

Source files are the files that contain the code that gets compiled. The implementation of your algorithm is contained in a source file. Header files contain code(usually function or class definitions ) that are copied into your source file by means of the #include preprocessor directive.

Can you have multiple header files?

Yes, you can use #include<bits/stdc++. h> This header file includes all the standard header files. The only problem is, it would include a lot of unnecessary files which you don't require for a program, and also, since it includes a lot of header files your compilation time will increase.


2 Answers

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.

like image 94
David Thornley Avatar answered Oct 15 '22 11:10

David Thornley


Logical, structured organisation and small source files enable:

  • faster, better programming - breaking the code into more manageable and understandable chunks makes it easier to find, understand and edit the relevant code.
  • code re-usability - different "modules" of code can be separated into groups of source/header files that you can more easily integrate into different programs.
  • better "encapsulation" - only the .c files that specifically include that header can use the features from it, which helps you to minimise the relationships between different parts of your code, which aids modularity. It doesn't stop you using things from anywhere, but it helps you to think about why a particular c file needs to access functions declared in a particular header.
  • Aids teamwork - two programmers trying to change the same code file concurrently usually cause problems (e.g. exclusive locks) or extra work (e.g. code merges) that slow each other down.
  • faster compiles - if you have one header then every time you make a change in it you must recompile everything. With many small headers, only the .c files that #include the changed header must be rebuilt.
  • easier maintainability & refactoring - for all the above reasons

In particular, "one header for each source file" makes it very easy to find the declarations relevant to the c file you are working in. As soon as you start to coalesce multiple headers into a single file, it starts to become difficult to relate the c and h files, and ultimately makes building a large application much more difficult. If you're only working on a small application then it's still a good idea to get into the habit of using a scalable approach.

like image 20
Jason Williams Avatar answered Oct 15 '22 12:10

Jason Williams