I understand the header file contains all the prototypes of every function contained in the implementation file; but what is the real purpose? Is it because when another program calls on the class, it only receives the information in the header file, and the implementation is kept hidden? What is the real purposes of this?
-
The header should only contain the prototypes of functions accessible outside the implementation file. It also contains the type information needed to use those functions. Header files are the glue that hold the system together; they ensure that the consumers and producers are in agreement. It would also be damn irritating to have to type out all the information in the standard headers every time you need to use one of the standard facilities. – Jonathan Leffler Apr 04 '14 at 18:55
-
BTW, if it were me, I'd have accepted another answer in that thread. I can't even imagine re-compiling projects without header files. ) – raina77ow Apr 04 '14 at 18:57
-
@raina77ow agreed, there's more higher level to it then they really get into which IMHO better explains why you wouldn't need to recompile as a result.. Thanks for pulling in the dupe none the less. Think this was like the 3rd one this week on this exact question. – UpAndAdam Apr 04 '14 at 20:02
1 Answers
The purpose is that when someone gives you a library, they don't always give you the code. And frankly you don't want to know about it. You simply need to know the function prototypes and data structures it provides, which you obtain from the Header File. And then you link to the library.
Here's an example that might help make some sense of it for you:
I publish a library libfoo
(doesn't matter if its a .so, .a or .dll) and told you it had a bunch of good functions and data type in it that solve a problem and you want to use it. If I didn't give you the header file, how would your code possibly compile when it would need to know how big data structures are / what function signatures to look for? How would you know how to code it?
The library won't be referenced until linking, and compilation is already complete at that point in time.

- 4,515
- 3
- 28
- 46