What stages does the g++ driver run, and where do the -E, -S and -c options stop it?
answer
- a driver, not one program
- four stages, always in order
- .ii, then .s, then .o
- -E writes to standard output
- gcc does not add the C++ library
basics
~20 sg++ is a driver that runs the preprocessor, the compiler proper, the assembler and the linker in that order. -E stops after preprocessing, -S after producing assembly (.s), and -c after assembling an object file (.o).
solid answer
~40 s`g++` is a **driver**: it does no compiling itself but runs up to four stages in a fixed order. The preprocessor expands `#include` and macros; the compiler proper (`cc1plus`) turns the preprocessed translation unit into assembly; the assembler `as` turns that into a relocatable object file; and the linker combines objects and libraries into an executable. `-E` stops after preprocessing and writes the result to standard output; `-S` stops after compilation and writes `foo.s`; `-c` stops after assembly and writes `foo.o`. Without any of them the driver links, and the output defaults to `a.out` unless `-o` names it. Use `g++` rather than `gcc` to link C++: both compile `.cpp` files as C++, but only `g++` adds the C++ standard library to the link.
code
bash · 6 linesg++ -E main.cpp > main.ii # preprocess only: includes pasted, macros expanded
g++ -S -O2 main.cpp # stop after compilation: writes main.s
g++ -c -O2 main.cpp # stop after assembly: writes main.o
g++ -c -O2 util.cpp # writes util.o
g++ main.o util.o -o app # link step only: objects in, executable out
g++ -### main.cpp -o app # print the commands the driver would rungo deeper
Name the four stages in order and say what -E, -S and -c stop after and what file each leaves behind.
Explain why builds compile with -c per file and link once, and why linking C++ through gcc misses the C++ standard library.
Use -v, -### and -save-temps to reproduce a single build step outside the build system when a compile behaves differently on two machines.
Treat the exact driver invocation as part of the build contract: pin the driver, record the full command lines, and make every stage reproducible from them.
## The driver is not the compiler When you type `g++ main.cpp -o app`, the program you run is a **driver**. It reads the options, decides which stages each input needs, and starts a separate program for each stage, passing temporary files between them. GCC's manual states the rule directly: compilation can involve up to four stages, **preprocessing, compilation proper, assembly and linking, always in that order**. You can watch the driver work: - `g++ -v main.cpp` prints every command it executes, including the compiler proper (`cc1plus` for C++), the assembler and the link step. - `g++ -### main.cpp` prints the same commands **without running them**, quoted so a script can capture them. ## The four stages and their outputs | Stage | Program | Input | Output | Stop here with | |---|---|---|---|---| | Preprocessing | built into `cc1plus` | `main.cpp` | preprocessed C++ (`.ii`) | `-E` | | Compilation proper | `cc1plus` | preprocessed source | assembly (`.s`) | `-S` | | Assembly | `as` | `.s` | relocatable object (`.o`) | `-c` | | Linking | `collect2` / `ld` | `.o` files and libraries | executable or shared library | (default) | 1. **Preprocessing** pastes in every `#include`, expands macros and evaluates `#if`. The result is one large translation unit with line markers such as `# 1 "main.cpp"` so later diagnostics still point at the original files. 2. **Compilation proper** parses and type-checks C++, instantiates templates, optimizes and emits assembly text for the target CPU. 3. **Assembly** encodes that text into machine code in an object file: sections such as `.text`, `.data` and `.bss`, a symbol table, and relocations for every address still unknown. 4. **Linking** resolves those undefined symbols against other objects and libraries and lays out the final image. ## Stopping early: -E, -S and -c - `-E` stops after preprocessing. The output goes to **standard output** unless `-o` names a file, which is why `g++ -E main.cpp > main.ii` is the common spelling. - `-S` stops after compilation proper and writes `main.s`, named after the source, by default. Add `-O2` to see optimized code. - `-c` stops after assembly and writes `main.o`. This is what every build system does per source file, so that only changed files are recompiled and the link runs once at the end. GCC also ignores inputs a stage does not need: `g++ -c util.o` has nothing to compile, so the object file is simply ignored. ## -o and the default names `-o file` names the **primary output**, whatever the stage produced: an executable, an object, an assembly file or preprocessed source. Without `-o` the defaults are: - `a.out` for a linked executable; - `source.o` for `-c` and `source.s` for `-S`; - standard output for `-E`. Because `-o` names a single output, it is normally paired with one input file when you stop early. ## g++ versus gcc Both drivers recognise `.cpp`, `.cc`, `.cxx` and `.C` as C++ and compile them with `cc1plus`. The difference is at the edges: - **Linking:** the manual says the use of `gcc` does not add the C++ library, while `g++` automatically links against it. Linking C++ objects with `gcc` therefore fails with undefined references to `std::` and runtime symbols such as `operator new`. - **Ambiguous suffixes:** `g++` treats `.c`, `.h` and `.i` files as C++; `gcc` treats them as C. The rule of thumb: compile and link C++ with `g++` (often also installed as `c++`). ## What an object file holds The `.o` that `-c` leaves behind is a **relocatable object**, not a small program. On ELF platforms it contains: - **sections**: `.text` for machine code, `.data` for initialized globals, `.bss` for zero-initialized ones, `.rodata` for constants, plus debug sections when `-g` is used; - a **symbol table** listing what the file defines and what it still needs; - **relocations**: for every instruction or data word whose final address is not known yet, a note telling the linker what to patch. `nm -C file.o` shows the symbol table. Defined functions appear with `T`, defined data with `D` or `B` (lower case when the symbol is file-local, such as a `static` variable), and every symbol the file uses but does not define with `U`. Each `U` line is a promise the link step must keep; when no input defines it, the link fails with an undefined reference, which the material on linking takes from there. C++ names appear mangled (`_Z6helperi`) unless `-C` demangles them. ## Keeping the intermediate files `-save-temps` keeps what the driver normally deletes, so `g++ -save-temps -c main.cpp` leaves `main.ii`, `main.s` and `main.o` side by side. It is the quickest way to inspect exactly what one build line fed to each stage, and it is the subject of the debugging material on preprocessed and assembly output.
- Why do build systems compile each file with -c instead of passing all sources to one g++ command?Each `-c` produces an independent object, so after an edit only the changed translation units are recompiled and the cheaper link step reruns. Separate commands also run in parallel across cores. A single `g++ a.cpp b.cpp -o app` recompiles everything every time.
- What does g++ -v show that helps when a build behaves differently on two machines?`-v` prints the driver version and every subprocess command line, including the include search list and the exact linker invocation with its implicit libraries and startup files. Comparing the two outputs usually reveals a different compiler, include directory or library path.
- g++ is the compiler itself rather than a driver that runs several programs.
- -c means compile and link in one step.
- -E writes a .ii file next to the source by default.
- gcc and g++ are identical, so linking C++ with gcc is fine.
- -S produces machine code rather than assembly text.