Headers and Includes: Why and How
Pages: 1 2
Disch (13725)
*************************************************
** 0) Introduction **
*************************************************
This article is meant to address a common new bie problem regarding failure to understand #include, headers,
and source file interaction. Several good practices are outlined and explained to show how to avoid some ugly
snags. Later sections get into more advanced topics (inlining and templates), so even C++ coders with some
experience under their belt might benefit from a read!
If you are already familiar with the basics, feel free to skip ahead to section 4. That is where practices and
design strategies are discussed.
*************************************************
** 1) Why we need header files. **
*************************************************
If you're just starting out in C++, you might be wondering why you need to #include files and why you would
want to have multiple .cpp files for a program. The reasons for this are simple:
(1) It speeds up compile time. As your program grows, so does your code, and if everything is in a single file,
then everything must be fully recompiled every time you make any little change. This might not seem like a big
deal for small programs (and it isn't), but when you have a reasonable size project, compile times can take
several minutes to compile the entire program. Can you imagine having to wait that long betw een every minor
change?
Compile / wait 8 minutes / "oh crap, forgot a semicolon" / compile / wait 8 minutes / debug / compile / wait 8
minutes / etc
(2) It keeps your code more organized. If you seperate concepts into specific files, it's easier to find the code
you are looking for when you want to make modifications (or just look at it to remember how to use it and/or
how it works).
(3) It allow s you to separate interface from implementation. If you don't understand what that means, don't
worry, we'll see it in action throughout this article.
Those are the upsides, but the big, obvious downside is that is makes things a little more complicated if you
don't understand how it all works (in reality, though, it's simpler than the alternatives as the project gets
larger)
C++ programs are built in a two stage process. First, each source file is compiled on its ow n. The compiler
generates intermediate files for each compiled source file. These intermediate files are often called object files --
but they are not to be confused with objects in your code. Once all the files have been individually compiled, it
then links all the object files together, which generates the final binary (the program).
This means that each source file is compiled separately from other source files. As a result of this, in terms of
compiling, "a.cpp" is clueless as to what's going on inside of "b.cpp". Here's a quick example to illustrate:
1
2
3
4
5
6
7
8
9
10
11
12
13
// in myclass.cpp
class MyClass
{
public:
void foo();
int bar;
};
void MyClass::foo()
{
// do stuff
}
1
2
3
4
5
6
7
// in main.cpp
int main()
{
MyClass a; // Compiler error: 'MyClass' is unidentified
return 0;
}
Even though MyClass is declared in your program, it is not declared in main.cpp, and therefore when you compile
main.cpp you get that error.
This is where header files come in. Header files allow you to make the interface (in this case, the class MyClass)
Search: