Tuesday, April 19, 2011

[discussion_vu] CS301_Header Files_MakeFile_Information



---------- Forwarded message ----------
From: ..::ISHFAQ::.. <mc100402092@vu.edu.pk>
Date: Wed, Apr 20, 2011 at 6:48 AM
Subject: CS301_Header Files_MakeFile_Information
To: zavia-lms@googlegroups.com, afaaq <afaaqtariq233@gmail.com>, 

اسلام علیکم ورحمتہ اللہ و برکاتہ

 Header files and Using a makefile for Managing a C++ Programming Project

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:

1#include <iostream>
2int main()
3{

5    cout << "Hello, world!" << endl;
6    return 0;
7}

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 <iostream>, 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 won't link if it can't 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.

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 don't want to make their source code available for intellectual property reasons.

Writing your own header files

Now let's 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:

1int add(int x, int y)
2{
3    return x + y;
4}

main.cpp:

01#include <iostream>
02 
03 int add(int x, int y); // forward declaration using function prototype
04 
05 int main()
06 {

08    cout << "The sum of 3 and 4 is " << add(3, 4) << endl;
09    return 0;
10}

We'd 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).

Writing our own header files is surprisingly easy. Header files consist of two parts. The first part is called a header guard, which is discussed in the next lesson (on the preprocessor). The second part is the actual content of the .h file, which should be the declarations for all of the functions we want other files to be able to see. Our header files should all have a .h extension, so we'll call our new header file add.h:

add.h:

1#ifndef ADD_H
2#define ADD_H
3 
4int add(int x, int y); // <a class="ml-smartlink" href="http://en.wikipedia.org/wiki/Function_prototype">function prototype</a> for add.h
5 
6

No comments:

Post a Comment

PAID CONTENT