Login    New User    Forgot Password    Help      

Header files

GO TO INDEX

Header files


Code files (with a .cpp extension) are not the only files commonly seen in programs. The other type of file is called a header file, sometimes known as an include file. Header files almost always have a .h extension. The purpose of a header file is to hold declarations for other files to use.

Using standard library header files


Consider the following program: #include < iostream·h>
int main()
{
using namespace std;
cout << "Hello, world!" << endl;
return 0;
}

This program prints Hello, world! to the console using cout. However, our program never defines cout, so how does the compiler know what cout is? The answer is that cout has been declared in a header file called iostream. When we use the line #include , we are telling the compiler to locate and then read all the declarations from a header file named iostream.
Keep in mind that header files typically only contain declarations. They do not define how something is implemented, and you already know that your program wont link if it cant find the implementation of something you use. So if cout is only defined in the iostream header file, where is it actually implemented? It is implemented in the runtime support library, which is automatically linked into your program during the link phase.
IncludeLibrary
A library is a package of code that is meant to be reused in many programs. Typically, a library includes a header file that contains declarations for everything the library wishes to expose (make public) to users, and a precompiled object that contains all of the implementation code compiled into machine language. These libraries typically have a .lib or .dll extension on Windows, and a .a or .so extension on Unix. Why are libraries precompiled? First, since libraries rarely change, they do not need to be recompiled often, if ever. It would be a waste of time to compile them every time you wrote a program that used them. Second, because precompiled objects are in machine language, it prevents people from accessing or changing the source code, which is important to businesses or people who dont want to make their source code available for intellectual property reasons.

Writing your own header files


Now lets go back to the example we were discussing in the previous lesson. When we left off, we had two files, add.cpp and main.cpp, that looked like this:
add.cpp: int add(int x, int y)
{
return x + y;
}

main.cpp: #include < iostream·h>

int add(int x, int y); // forward declaration using function prototype
int main()
{
using namespace std;
cout << "The sum of 3 and 4 is " << add(3, 4) << endl;
return 0;
}

Wed used a forward declaration so that the compiler would know what add was when compiling main.cpp. As previously mentioned, writing forward declarations for every function you want to use that lives in another files can get tedious quickly.
Header files can relieve us of this burden. A header file only has to be written once, and it can be included in as many files as needed. This also helps with maintenance by minimizing the number of changes that need to be made if a function prototype ever changes (eg. by adding a new parameter).

GO TO INDEX