Broad Network


Basics of C++ Include Files

C++ Taking the Bull by the Horns – Part 20

Forward: In this part of the series, we look at the basics of the include preprocessing directive.

By: Chrysanthus Date Published: 22 Aug 2012

Introduction

This is part 20 of my series, C++ Taking the Bull by the Horns. In this part of the series, we look at the basics of the include preprocessing directive. This tutorial is applicable to the MinGW compiler suite and the windows operating system. A good number of the features here are the same in other systems. All the features here have equivalents in other compilers and/or operating systems. If you are not using the MinGW and windows operating system, then this tutorial will guide you on how to find the equivalents in your system.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Example
Consider the following code:

#include <iostream>
using namespace std;

    void myFn()
        {
            cout << "Output from Function.";
        }

    int main()
        {
            myFn();
        
            return 0;
        }

There are two functions here: the main function and the user defined function, myFn. Read and try the above code. You should have the output as "Output from Function.". We shall separate this file into two. The myFn function definition will be typed in a separate file. Type the myFn function definition as follows in a separate file with your text editor:

    void myFn()
        {
            cout << "Output from Function.";
        }

Your working directory is MinGW in drive C. We created this directory in the first part of the series and installed the MinGW compiler suite there. The installation process created a directory called, include, in the MinGW directory. Save the above file having the myFn function definition, with the name, atop.cpp in the include directory.

Modify your initial file as follows:

#include <iostream>
using namespace std;

#include <aTop.cpp>

int main()
    {
        myFn();
    
        return 0;
    }

In this initial modified file, the myFn function definition is no longer there; we have it in a separate file. In the modified initial file, the third line is a preprocessing directive. It is an example of what is called, the include preprocessing directive. The first line is also an include preprocessing directive, but we shall talk about that later. In the third line, you have the name, aTop.cpp of the file we have saved, in the angle brackets. Remember, this file has been saved in the include directory, which is a sub directory created by the installation process in the working directory.

What does the above include directive do? It gets the content of the file, aTop.cpp from the include directory and paste it at the position of the include preprocessing directive, during the compilation process; you do not notice it. So ultimately, the modified file is the same as the original file with the definition of the function, myFn present at its expected position. That is the purpose of the include directive. With the file, aTop.cpp in the include directory, try the above modified code. Your output should be as before.

In the include preprocessing directive, the file name is in angle brackets. It is good to avoid any space between the file name and any of the angle brackets.

Interface and Implementation
When dealing with functions, you have what is called Interface and Implementation. Read and try the following code.

#include <iostream>
using namespace std;

    void myFn(int anInt);

    void myFn(int anInt)
        {
            cout << anInt;
        }

    int main()
        {
            myFn(66);
        
            return 0;
        }

A prototype of a function is an expression that begins with the return object type of the function, then a space and the name of the function, then parentheses with parameters of the function and then a semicolon to form a statement. A function definition begins with this prototype but without the semicolon and then followed by the block of the function (with statements).

In the above code, you have the prototype of the function myFn, ending with a semicolon, then you have the function definition. The code works well. There is no problem with that. Know that it is possible to have a prototype in one position in the code and then have the function definition right down in the code after many other statements.

The prototype of a function is called the interface of the function. The definition is called the implementation of the function. A function prototype is also called the declaration of the function. Typically the interface and definition are in different files as illustrated below.

Header files
A header file with an extension, .hh (or .h) is a file that commonly has preprocessing define directives and function prototypes. It works in conjunction with the file that has the main function and with other files that you want it to work with. Any file that deals with the #define identifiers and function prototypes in the header file, should include (see below) the header file. The compiler will look for the header file from the include directory (you should put it there). Consider the following complete code:

#include <iostream>
using namespace std;

#define anInt

    void myFn()
        {
            int num = anInt 43;
            cout << num;
        }

int main()
    {
        myFn();
    
        return 0;
    }

In the above code, the main function calls the function myFn, which is outside the block of the main function. There is a define preprocessing directive at the top of the code. The identifier of this preprocessing directive is used by the myFn function. We can split this code into three. One will be the header file. The header file will have the define preprocessing directive and the prototype for the function, myFn. The content of the header file is

#define anInt

void myFn();

Type the above two lines in a text editor and save the file with the name, demo.hh in the include directory.

The other file will be the file having the main function. Its content is:

#include <iostream>
using namespace std;

#include <demo.hh>

int main()
    {
        myFn();
    
        return 0;
    }

Type the above in a text editor and save the file with the name, mainfile.cpp in the working directory, MinGW. The main function deals with the function, myFn whose prototype is in the header file, so the header file is included (in the third line) of the mainfile.cpp file.

The third file implements the header file. It assigns a value to the define preprocessing directive identifier of the header file and it defines the function of the function prototype of the header file. In a practical example, there will be many preprocessing define lines and function prototypes in the header file (whose implementations can be done in this third file). This is the content of the third file:

#include <iostream>
using namespace std;
#include <demo.hh>

    void myFn()
        {
            int num = anInt 43;
            cout << num;
        }

Type the above in a text editor and save the file with the name, demo.cpp in the working directory, MinGW. This third file implements what is in the header file, so it has to include the header file. I will say more about the iostream file later.

To compile these three files you will have to type the following at the DOS (command) prompt of your home directory:

    g++ mainfile.cpp demo.cpp -o complete.exe

In the command line, the mainfile.cpp file is typed first before the demo.cpp file. The resulting executable file is called, complete.exe; you can give it whatever name you want. You can try the above three files by typing the above command. The compiler looks for the include file in the include directory. You can extend the above to more than three files. Put your header files in the include directory and the non-header files and the main file in the working directory. In the DOS Prompt command, precede the –o switch with the names of the non-header files. The compiler will look for your header files in the include directory. Each non-header file should have the include directives for the header files. Note: There are important pre-installed files in the include directory, do not delete these pre-installed files.

Introduction to Library File
A library file is like the demo.cpp file relative to the other two files. However, it is compiled differently and it would be kept in the directory called, lib in the working directory. This lib directory was created by the installation of the MinGW tools. I will not go into the details of creating libraries in this basic tutorial series.


The iostream File
In order to use the cout object you need the iostream include file. The MinGW suite comes with at least two include directories. I have mentioned only one of them. The iostream file is in one of the include directories created during the installation process. Any include file is searched for by the compiler in all the include directories created by the installation. To allow this search, all you have to do is to have the include preprocessing directive in your file. All you need to do for simple coding, is to include the header files and include files you need in the non-header files and not worry about the corresponding library or libraries. Any file that uses the cout object must include the iostream file. That is why the mainfile.cpp file and the demo.cpp file above include the iostream file.

Well, let us end here. We continue in the next part of the series.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message