The compiler, linker, make and CMake
What g++ actually does to a source file, how headers and libraries fit together, and what optimisation levels can and cannot do.
- Run the four compilation phases by hand and say what each produces
- Use header guards, inline variables, and static versus shared libraries
- Build with make and CMake, and read what an optimisation level changed
g++ hello.cpp -o hello looks like one step. It is four, and the repository’s hello.cpp runs them one at a time:
g++ hello.cpp -o hello.ii -E # preprocess
g++ hello.ii -o hello.s -S # compile to assembly
g++ hello.s -o hello.o -c # assemble to an object file
g++ hello.o -o hello # link
The numbers are striking. Preprocessing pastes in <iostream> and everything it includes: 9 lines become 36,593. Most of that is declarations, so the compiler emits only 63 lines of assembly. The object file still does not know where std::cout lives — that is resolved by the linker, against the shared libstdc++ (a 16 KB executable) or by copying the library in with -static (2.3 MB).
Headers, guards and inline variables
A header holds declarations that several .cpp files share. Including one twice in the same translation unit redefines everything in it. 04header_guards/ is set up to hit exactly that: s2.h includes s1.h, and main.cpp includes both, so without protection struct S1 would be defined twice. The #pragma once at the top of s1.h (or the classic #ifndef S1_H guard shown in its comments) makes the second inclusion a no-op, and the example compiles.
Guards do not help across different .cpp files. A global variable defined in a header and included by main.cpp and function.cpp is defined twice, and the linker rejects it. 05inline_variables/ fixes this with C++17’s inline int global_value = 0;, which tells the linker the definitions are the same variable. The program adds 10 in main and 10 in update_value and prints 20 — one shared variable.
Libraries
08static_libraries/ bundles add.o and multiply.o into libtest.a, which the linker copies into the executable. 09shared_libraries/ builds libtest.so instead, which is loaded at run time — smaller executables and one copy in memory for every program that uses it, at the price of needing the .so to be findable (LD_LIBRARY_PATH or an rpath) when the program starts.
make and CMake
A Makefile lists targets, their dependencies and the command that builds them; make rebuilds only what is older than its inputs. From make3/:
print_add: print_add.o add.o
g++ $^ -o $@
$^ is all the prerequisites and $@ the target. CMake sits one level higher: it describes what to build and generates the Makefiles (or Ninja files) for you. cmake5/ builds a shared library in one directory and links it from another:
add_library(math SHARED add.cpp multiply.cpp)
target_include_directories(math PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
add_executable(main main.cpp)
target_link_libraries(main math)
PUBLIC propagates the include directory to everything that links math, so main needs no include path of its own.
What the optimiser did
01compiler_optimization/main.cpp is meant to compare optimisation levels on a loop that takes every element of a 228-element vector modulo 20:
std::vector<int> vector(num_elements);
for (auto value : vector) {
value %= 20;
}
Two details change what it measures. First, auto value is a copy of each element, so the loop modifies copies and the vector never changes; auto &value was intended. The loop has no observable effect, and at -O1 and above g++ deletes it: the -O3 binary’s main is just operator new, memset, operator delete. The timing then measures allocating and zeroing 1 GiB, not the modulo.
Second, the command in the comment is g++ main.cpp -o main_opt_3_native -o3 -march=native. Lowercase -o3 is not an optimisation flag but a second output name: g++ writes the executable to a file called 3, at the default -O0. The flag is capital -O3.
Try it yourself. Fix the loop to auto &value, build at -O0 and -O3, and compare objdump -d of main for both: find the division (or the multiply-by- magic-constant that replaces it). Then time both with time ./a.out.