A Quick Look at Makefiles
You can imagine how tedious the build process could be if you had a large number of source code files for a particular project. Manually entering individual compiler and linker commands on the command line becomes tiresome very quickly. In order to avoid this, a makefile can be used. A makefile is a script that tells the make utility how to build a particular program. (The make utility is typically installed with the other GNU tools.) The make utility follows the rules in the makefile in order to automatically generate output files from a set of input source files.
Makefiles might be a bit of a pain to set up, but they can be a great timesaver and a very powerful tool when building project files over and over (and over) again. Having a sample available can reduce the pain of setting up a makefile.
The basic layout for a makefile build rule is:
target: prerequisite
commandThe target is what is going to
be built, the prerequisite is a file
that must exist before the target can
be created, and the command is a
shell command used to create the target. There can be multiple prerequisites on the target line (separated by
white space) and/or multiple command lines. But be sure to put a tab,
not spaces, at the beginning of every line containing a command.
Here’s a makefile for building our Blinking LED program:
XCC = arm-elf-gcc LD = arm-elf-ld CFLAGS = -g -c -Wall \\ -I../include LDFLAGS = -Map blink.map -T viperlite.ld -N all: blink.exe led.o: led.c led.h $(XCC) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access