Getting the basics of linking right
We discussed the life cycle of a C++ program in Chapter 5, Compiling C++ Sources with CMake. It consists of five main stages – writing, compiling, linking, loading, and execution. After correctly compiling all the sources, we need to put them together into an executable. Object files produced in a compilation can't be executed by a processor directly. But why?
To answer this, let's take a look at how a compiler structures an object file in the popular ELF format (used by Unix-like systems and many others):
The compiler will prepare an object file for every unit of translation (for every .cpp
file). These files will be used to build an in-memory image of our program. Object files contain the following elements:
- An ELF header identifying the target operating system, ELF file type, target instruction set architecture, and information on the position...