3.5. Linking Modules

This page is only relevant to programs that contain two or more modules. Leave it until you need it.


Modules

Only small computer programs are written as a single file. Larger programs are normally broken into separate modules, each module in one file. When you add more to softward, instead of making the files larger, you add more files.

There are two issues that need to be dealt with to link C++ modules together into a single executable file: header files and linkage.


Header files

You normally compile each module separately, using the -c option to create an object file for each. When compiling one file, the compiler needs to know about the resources that other files will provide, once the entire executable file is produced.

For example, suppose that A.cpp defines function jump and B.cpp wants to use jump. The compiler needs to know that jump will be available after the object files are combined. It also needs to know information about jump such as how many arguments it takes, what the argument types are and what type of value jump returns.

To provide the required information, each file that defines (or exports) functions for other modules to use has a header file describing those functions. The header file for A.cpp is normally called A.h. File A.h typically contains prototypes for the functions that A.cpp exports. (If A.cpp defines functions that are only for its own use, then those functions are not mentioned in file A.h.)

Header files address both of the issues mentioned above. They are much shorter and less time-consuming for a compiler to read than are full C++ files. And you can omit information from them that you prefer to keep hidden.

Each module that wants to use the resources provided by A.cpp includes A.h, using line

 #include "A.h"
A.cpp also always includes A.h. That is important for two reasons. First, A.h often contains definitions of types that are not repeated in A.cpp, Second, you always want to ensure that you have not made a mistake. By including A.h in A.cpp you allow the compiler to check that the prototypes in A.h are consistent with the definitions in A.cpp.

See sample header file gcd.h and file gcd.cpp.


Header files for the library

You will need to use resources provided by the C++ library, and in order to do that you must include their header files. Header files in the C++ library do not have .h in the names. Also, you should use <...> instead of "..." in the #include line, to tell the compiler to look in the library rather than in the current working directory. For example,

  #include <cstdio>
includes the header file called cstdio from the C++ library.

You will also want to have line

  using namespace std;
to allow you to use the names defined in the library. (Yes, this is awkward, but you will want to do it.) For example, to use the cmath and cstdio libraries, write
  #include <cmath>
  #include <cstdio>
  using namespace std;

Note. There must be no semicolon at the end of an #include line. But there must be a semicolon at the end of a using ... line.

Note. The style just described works for C++ but not for C, and older language. C programs use a simpler form. In a C program, the library header files have names that end on .h. You also do not have a using namespace std line. For example, a C program would say

  #include <math.h>
  #include <stdio.h>


Linkage

In order to create an executable file from object files, you need to perform a step called linkage to put the object files together and write the executable file. The g++ compiler knows how to do linkage. Just list the object files to link. For example, command

  g++ -o run main.o tools.o grape.o
creates executable file run by linking together object files main.o, tools.o and grape.o.


Compiling several files at once

The g++ compiler will compile several files at once if you prefer. For example, command

  g++ -Wall -o run main.cpp tools.cpp grape.cpp
compiles main.cpp, tools.cpp and grape.cpp (each separately) and then links them together and writes executable file run. That is not the preferred way of doing it (see make), but it works in a pinch.


Exercises

  1. What is the purpose of a header file? Answer

  2. How does a C++ program include header file tools.h that is in the current directory? Answer

  3. How can you link together object files tools.o and main.o, producing an executable file called go? Answer

  4. What is the difference between

      #include "tools.h"
    
    and
      #include <tools.h>
    
    Answer