tags:

views:

5965

answers:

8

I often wondered: why does C++ have header files and cpp files?

+1  A: 

It's the preprocessor way of declaring interfaces. You put the interface (method declarations) into the header file, and the implementation into the cpp. Applications using your library only need to know the interface, which they can access through #include.

Martin v. Löwis
+13  A: 

Because in C++, the final executable code does not carry any symbol information, it's more or less pure machine code.

Thus, you need a way to describe the interface of a piece of code, that is separate from the code itself. This description is in the header file.

unwind
+18  A: 
MadKeithV
Not really true. The header still contains a major part of the implementation. Since when were private instance variables part of a class's interface? Private member functions? Then what the hell are they doing in the publicly visible header? And it falls further apart with templates.
jalf
That's why I said that it's not perfect, and the Pimpl idiom is needed for more separation. Templates are a whole different can of worms - even if the "exports" keyword was fully supported in most compilers it would still me syntactic sugar rather than real separation.
MadKeithV
@MadKeithV: How do other languages handle this? for example - Java? There is no header file concept in Java.
Lazer
In Java the concept of "interfaces" is more strongly supported. In my eyes these interfaces are a further development of the original idea of the "header" file - containing *only* the 'what'.
MadKeithV
@Lazer: Java is simpler to parse. The Java compiler can parse a file without knowing all the classes in other files, and check the types later. In C++ lots of constructs are ambiguous without type information, so the C++ compiler needs information about referenced types to parse a file. That's why it need headers.
nikie
+4  A: 

Because the people who designed the library format didn't want to "waste" space for rarely used information like C preprocessor macros and function declarations.

Since you need that info to tell your compiler "this function is available later when the linker is doing its job", they had to come up with a second file where this shared information could be stored.

Most languages after C/C++ store this information in the output (Java bytecode, for example) or they don't use a precompiled format at all, get always distributed in source form and compile on the fly (Python, Perl).

Aaron Digulla
Wouldn't work, cyclic references. I.e.you can't build a.lib from a.cpp before building b.lib from b.cpp, but you can't build b.lib before a.lib either.
MSalters
Java solved that, Python can do it, any modern language can do it. But at the time when C was invented, RAM was so expensive and scarce, it just wasn't an option.
Aaron Digulla
+1  A: 

Often you will want to have a definition of an interface without having to ship the entire code. For example, if you have a shared library, you would ship a header file with it which defines all the functions and symbols used in the shared library. Without header files, you would need to ship the source.

Within a single project, header files are used, IMHO, for at least two purposes:

  • Clarity, that is, by keeping the interfaces separate from the implementation, it is easier to read the code
  • Compile time. By using only the interface where possible, instead of the full implementation, the compile time can be reduced because the compiler can simply make a reference to the interface instead of having to parse the actual code (which, idealy, would only need to be done a single time).
Dan
Why couldn't library vendors just ship a generated "header" file? A pre-processor free "header" file should give much better performance (unless the implementation was really broken).
Tom Hawtin - tackline
I think its irrelevant if the header file is generated or hand written, the question wasn't "why do people write header files themselves?", it was "why do we have header files". The same goes for preprocessor free headers. Sure, this would be faster.
Dan
+25  A: 

C++ compilation

A compilation in C++ is done in two major phases:

The first is the compilation of "source" text files into binary "object" files:

The CPP file is the compiled file, and is compiled without any knowledge about the other CPP files (or even libraries), unless fed to it through raw declaration or header inclusion. The CPP file is usually compiled into a .OBJ or a .O "object" file.

The second is the linking together of all the "object" files, and thus, the creation of the final binary file (either a library or an executable).

Where does the HPP fit in all this process?

A poor lonesome CPP file...

The compilation of each CPP file is independant from all other CPP files, which means that if A.CPP needs a symbol defined in B.CPP, like:

// A.CPP
void doSomething()
{
   doSomethingElse() ; // defined in B.CPP
}

// B.CPP
void doSomethingElse()
{
   // Etc.
}

It won't compile because A.CPP has no way to know "doSomethingElse" exists... unless there is a declaration in A.CPP, like:

// A.CPP
void doSomethingElse() ; // from B.CPP

void doSomething()
{
   doSomethingElse() ; // defined in B.CPP
}

Then, if you have C.CPP which uses the same symbol, you then copy/paste the declaration...

COPY/PASTE ALERT !

Yes, there is a problem. Copy/Pastes are dangerous, and difficult to maintain. Which means that it would be cool if we had some way to NOT copy/paste, and still declare the symbol... How can we do it? By the include of some text file, which is commonly suffixed by .h, .hxx, .h++ or, my preferred for C++ files, .hpp:

// B.HPP (here, we decided to declare every symbol defined in B.CPP)
void doSomethingElse() ;

// A.CPP
#include "B.HPP"

void doSomething()
{
   doSomethingElse() ; // defined in B.CPP
}

// B.CPP
#include "B.HPP"

void doSomethingElse()
{
   // Etc.
}

// C.CPP
#include "B.HPP"

void doSomethingAgain()
{
   doSomethingElse() ; // defined in B.CPP
}

Conclusion

The header file is thus necessary because the C++ compiler is unable to search for symbol declarations alone, and thus, you must help it by including those declarations.

One last word: You should put header guards around the content of your HPP files, to be sure multiple inclusions won't break anything, but all in all, I believe the main reason for existence of HPP files is explained above. ^_^

paercebal
Wow--very informative and interesting to a non-c++ reader.
I Have the Hat
Cool :) I was thinking about this lately..'why the hell has C++ those header files' now I know..thanks :)
drax
[An excellent GameDev article explaining how to use header files](http://www.gamedev.net/reference/articles/article1798.asp).
Lazer
+5  A: 

Because C, where the concept originated, is 30 years old, and back then, it was the only viable way to link together code from multiple files.

Today, it's an awful hack which totally destroys compilation time in C++, causes countless needless dependencies (because class definitions in a header file expose too much information about the implementation), and so on.

jalf
A: 

Because C++ inherited them from C. Unfortunately.

andref