Broad Network


C++ Source File Inclusion

C++ Preprocessing Directives – Part 2

Forward: In this part of the series, I explain how you can join more than one continuous source code program file to effectively form one long file; I also comment on the limitation on including files in such a way.

By: Chrysanthus Date Published: 18 Sep 2012

Introduction

This is part 2 of my series, C++ Preprocessing Directives. In this part of the series, I explain how you can join more than one continuous source code program file to effectively form one long file; I also comment on the limitation on including files in such a way.

Example
Consider the following program:

#include <iostream>
using namespace std;

class Calculator
    {
        public:
        int num1;
        int num2;

        Calculator()
            {
                num1 = 11;
                num2 = 12;
            }

        int add ();
    };

int Calculator::add ()
            {
                int sum = num1 + num2;
                return sum;
            }

int main()
    {
        Calculator myObject;
        int result = myObject.add();
        cout << result;

        return 0;
    }

This is the program of a simple calculator the adds two numbers. It uses OOP (C++ object oriented programming - class). The program can be broken into three files as follows:

#include <iostream>
using namespace std;

class Calculator
    {
        public:
        int num1;
        int num2;

        Calculator()
            {
                num1 = 11;
                num2 = 12;
            }

        int add ();
    };

Let us call this file, first.cpp. The next portion can be:

int Calculator::add ()
            {
                int sum = num1 + num2;
                return sum;
            }

Let us call this file, second.cpp. The last portion can be:

int main()
    {
        Calculator myObject;
        int result = myObject.add();
        cout << result;

        return 0;
    }

Let us call this file, third.cpp. This file is actually the main file as it has the main method. As you can see, the contents of the three files can be joined together to form the one effective program above. This can be achieved using what is known as the #include directive.

Now, each compiler comes with one or more default include directories. This means that if you place a file in such a default directory, and you use the #include directive in the right way in the main file, assuming that the main file is in the working directory, when you compile the main file, the compiler will automatically look for the file in the include default directory and include the file to the main file. It would result as if the two files where written as one file.

For the compiler to look for a file in an include default directory, the syntax for the #include directive is:

#include <filename>

This directive is on its own line, like any other preprocessing directive. Any code that follows, must be typed in lines that follow, after the Enter key has been pressed. Remember, there is no semicolon, after any preprocessing directive. The filename typed in the syntax must have an extension (e.g. .cpp).

Including Files
For the above three-file example, the main file, third.cpp would be in the current working directory; the other two files, first.cpp and second.cpp, would be in any of the default include directories. For my compiler, the MinGW compiler, there are two default directories; each of them is called the, "include" directory, but they are in different locations (have different directory paths). You can place the other files in any of the default directories.

In this case (three-file example), you need two include directives, which are typed in the main file. So, for my compiler, the main file would be:

#include <first.cpp>
#include <second.cpp>

int main()
    {
        Calculator myObject;
        int result = myObject.add();
        cout << result;

        return 0;
    }

Each include directive and its filename, is where the content of the corresponding file would fit into the main file, effectively forming the complete long program.

In practice, the main file would be as follows, where the "#include <iostream>" and "using namespace std;" lines have been removed from the first file and pasted in the main file:

#include <iostream>
using namespace std;
#include <first.cpp>
#include <second.cpp>

int main()
    {
        Calculator myObject;
        int result = myObject.add();
        cout << result;

        return 0;
    }

Limitation of the include File Feature
If you are writing a long program in the light of the three-file example above, and you continue to include files to the main file, a point will reach where the resulting program will stop working. There is a maximum number of bytes that the resulting file of the include file technique, can have. You can actually write a program that is longer than this maximum number of bytes with a modified include file technique. You still use the include directive (see details later).

In fact, if you have a big program to write, it should not be in the form of a very long file as in the above three-file example. It should be in the form of almost independent files. There is a kind of C++ file called, the header file. To join the almost independent files together, you would need a few header files. I will talk about header files and the joining technique in a different series. Just search my blog if you want to know about that now. With the modified technique, you end up with a bigger program than if you had a long continuous program which consists of small dependent programs.

Non-default include Directory
What about the case, where the files to be included are not in the default include directory? In this case, instead of using angle brackets, you would use double quotes.

If you are dealing with an absolute path, you would have something like:

#include "c:dir1dir2file.ext"

If the file to be included is in the current working directory, you would have something like:

#include "file.ext"

If the file is in a directory relative to the current working directory, then you would have something like:

#include "dir3dir4file.ext"

where dir3 is an immediate sub directory to the current working directory.

Remember, any preprocessing directive ends with the pressing of the keyboad Enter key (no semicolon).

That is it for this part of the series. We stop here and continue in the next part.

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