3.3. Linking Modules


Modules

Only small computer programs are written as a single file. Larger programs are normally broken into separate modules, each module in one file. 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 provide.

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 type information about jump, such as how many arguments it takes, what the argument types are and what type of value it returns. But, while compiling B.cpp, the compiler does not look at A.cpp.

To provide the required information, each file that defines 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 it defines that other modules are allowed to use. (If A.cpp defines functions that are only for its own use, then those functions are not mentioned in the header file.)

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.


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 "...". That tells the compiler to look in the library; quotes tell the compiler to look in the current working directory (by default). 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. For example, to use the math and cstdio libraries, write
  #include <cmath>
  #include <cstdio>
  using namespace std;

Note. There must be no semicolon at the end of a #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. 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++ -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