From source to executable
A C program's build chain: preprocessor, compilation, assembly, linking and loading, the .c, .h, .i, .s and .o files, static and dynamic libraries, gcc's -E, -S, -c, -o and -l options, and the difference between compiled, interpreted and virtual machine.
- 30 cards
- 5 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- Scenarios
- Understand
A few cards from the deck
Compilation pipeline: what is it?
Show the answer
The series of tools that turn a C program into an executable: preprocessor, compiler, assembler, linker.
The
gcccommand chains them, but each stage has its own intermediate file and its own errors: knowing which one failed guides the diagnosis.Which file does the command
gcc -c main.cproduce?- main.o
- a.out
- main.s
- main.i
Show the answer
main.o
-ccompiles and assembles without linking: the result is the object filemain.o.a.outis the default name of a complete executable;main.scomes from-S, and the preprocessed text from-E.gccoptions stop the chain at a given stage:-Eafter the …,-Safter compilation,-cafter ….Show the answer
gccoptions stop the chain at a given stage:-Eafter the preprocessor,-Safter compilation,-cafter assembly.-Sleaves a.sfile in assembly language,-ca.ofile. Without these options,gccgoes all the way to the executable.Preprocessor: what is it?
Show the answer
The first stage, purely textual: it replaces
#includelines with the files' content, expands#defineand strips comments.It does not understand C:
gcc -E main.cshows the text it hands to the compiler, already several hundred lines for a single#include <stdio.h>.A program that calls
sqrtcompiles, but linking printsundefined reference to 'sqrt'. Which fix is needed?- Add
#include <math.h>at the top of the file - Add the
-Eoption to expand macros - Recompile with
-cto regenerate the object file - Add
-lmat the end of thegcccommand
Show the answer
Add
-lmat the end of thegcccommandIf
math.hwere missing, the compiler would be the one to report it (implicit declaration ofsqrt). Here the declaration is known; it is the definition, in the maths library, that the linker cannot find.-lmgoes after the source files: placed before them, the linker may skip the library and the error remains.- Add