25C. Linkage

Header files only tell the compiler what each module exports to other modules. There is another step in building a multi-module piece of software: linkage.

Modules are normally compiled separately into machine language files. All of the files making up the software are put together by a linker to make a complete executable file.

But a module does not know where in the complete executable program to find functions and variables that are part of other modules. The compiler inserts external references into the individual machine language files, and the linker fills them in with the memory addresses where functions and variables can be found.

Compiling and linking in one step

The g++ compiler will compile and link several files at once. 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.

Separate compilation and linkage

The preferred way of building a multi-module piece of software is to compile each module in a separate invocation of g++, and to do the linkage separately too. The advantage is that, if you only change one of the files, you only need to recompile that file and relink. You can appreciate the savings by imagining that there are 1000 modules.

Command

  g++ -Wall -c tools.cpp
compiles tools.cpp and creates file tools.o, a machine language file containing external references. G++ will invoke the linker for you. Command
  g++ -Wall -o run main.o tools.o grape.o
links previously compiled files main.o, tools.o and grape.o, creating executable file run.

Make

It should be obvious that you do not want to type in commands to compile every file that needs compiling. That is the kind of thing that should be automated. And, of course, it is. There is a tool called make that recompiles all files that need compiling, and only those files. Make needs to get instructions on how to compile and link a piece of software, which it gets from a file called Makefile.

Each assignment has a Makefile provided for you. I recommend that you use it.

Do not include .cpp files

It is tempting to link files by including all of the .cpp files in one of the modules, say main.cpp, as in the following.

  #include "A.h"
  #include "B.h"
  #include "C.h"
  #include "A.cpp"
  #include "B.cpp"
  #include "C.cpp"
  ...

  int main()
  {
    ...
  }
That way, you can compile your entire program by only compiling main.cpp.

As noted above, that is not the preferred way to link software, since you must recompile all modules if one module is changed.

For this course, there is a good reason for not doing that. When I test your submissions, I will link them in the correct way. If you include .cpp files, there will be duplicate definitions, since functions defined in A.cpp will be part of main.o and A.o, which my tester link together.

That forces me to change your program in order to run it. It is not my job to fix mistakes in your software, and I will penalize you for forcing me to do that. I don't have time to fix your program. The standards require that no file includes a .cpp file.


Exercises

  1. How can you use the g++ compiler to compile tools.cpp and produces file tools.o? Answer

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