Compilation and Makefiles
COMP2521 20T2 ♢ Compilation and Makefiles ♢ [0/10]
Compilers are programs that
- convert program source code to executable form
- "executable" might be machine code or bytecode
The Gnu C compiler (
gcc
)
- applies source-to-source transformation (pre-processor)
- compiles source code to produce object files
- links object files and libraries to produce executables
clang
is an alternative C compiler (also available in CSE)
Note that dcc
and 3c
are wrappers around gcc
/clang
- providing more checking and more detailed/understandable error messages
- better run-time support (e.g. array bounds, use of dynamic memory)
COMP2521 20T2 ♢ Compilation and Makefiles ♢ [1/10]
Stages in C compilation: pre-processing, compilation, linking
COMP2521 20T2 ♢ Compilation and Makefiles ♢ [2/10]
When multiple C files are involved:
COMP2521 20T2 ♢ Compilation and Makefiles ♢ [3/10]
Compilation/linking with gcc
gcc -c Stack.c
gcc -c bracket.c
gcc -o rbt bracket.o Stack.o
Note that stdio,assert
included implicitly.
gcc
is a multi-purpose tool
- compiles (
-c
), links, makes executables (-o
)
COMP2521 20T2 ♢ Compilation and Makefiles ♢ [4/10]
Compilation process is complex for large systems.
How much to compile?
- ideally, what's changed since last compile
- practically, recompile everything, to be sure
The
make
command assists by allowing
- programmers to document dependencies in code
- minimal re-compilation, based on dependencies
COMP2521 20T2 ♢ Compilation and Makefiles ♢ [5/10]
Example multi-module program …
COMP2521 20T2 ♢ Compilation and Makefiles ♢ [6/10]
make
is driven by dependencies given in a Makefile
A dependency specifies
target : source1 source2 …
commands to build target from sources
e.g.
game : main.o graphics.o world.o
gcc -o game main.o graphics.o world.o
Rule: target is rebuilt if older than any sourcei
(applied recursively)
COMP2521 20T2 ♢ Compilation and Makefiles ♢ [7/10]
game : main.o graphics.o world.o
gcc -o game main.o graphics.o world.o
main.o : main.c graphics.h world.h
gcc -Wall -Werror -c main.c
graphics.o : graphics.c world.h
gcc -Wall -Werror -c graphics.c
world.o : world.c
gcc -Wall -Werror -c world.c
Things to note:
- A target (
game
, main.o
, …) is on a newline
- followed by a
:
- then followed by the files that the target is dependent on
- The action (
gcc …
) is always on a newline
- and must be indented with a TAB
COMP2521 20T2 ♢ Compilation and Makefiles ♢ [8/10]
If make
arguments are targets, build just those targets:
prompt$ make world.o
gcc -Wall -Werror -c world.c
If no args, build first target in the Makefile
.
prompt$ make
gcc -Wall -Werror -c main.c
gcc -Wall -Werror -c graphics.c
gcc -Wall -Werror -c world.c
gcc -o game main.o graphics.o world.o
COMP2521 20T2 ♢ Compilation and Makefiles ♢ [9/10]
Makefiles
can contain "variables"
- e.g.
CC
, CFLAGS
, LDFLAGS
- can easily change which C compiler used, etc
make
has rules, which allow it to interpret e.g.
Stack.o : Stack.c Stack.h
as
Stack.o : Stack.c Stack.h
$(CC) $(CFLAGS) -c Stack.c
COMP2521 20T2 ♢ Compilation and Makefiles ♢ [10/10]
Produced: 6 Jun 2020