We can make use of Makefile features to only recompile our code when the source files changed. This can be achieved because make
stats the file system to check whether the target is older than it’s dependencies.
Makefile example
all: main
main: main.o
gcc main.o -o main
main.o: main.c
gcc -c main.c
The code above is a toy example. The name before the colon is our target and the name of the file generated by the command bellow it. The name or names after the colon are target’s dependencies.
Running make
the first time generates our output, named main
in this case. Running make
again we get a message stating that our target is up to date make: 'main' is up to date.
Please note that this is just a motivation about one feature of make
build system, many improvements can be made on the example to reach a production level Makefile.