As you remember in our previous post the lines we need to write in our terminal to build the most simple program has been increasing considerably and that represent a problem, it is very tedious and error prom.
Instead of writing the build instructions in our terminal we are going to create a new file with the name makefile and write all the build instructions there
NOTE: makefiles uses TABS instead of spaces, never forget it.
all:
arm-none-eabi-gcc -c driver/sum.c -o Build/sum.o -mcpu=cortex-m0plus -I driver
arm-none-eabi-gcc -c main.c -o Build/main.o -mcpu=cortex-m0plus -I driver
arm-none-eabi-gcc Build/main.o Build/sum.o -o Build/main.elf -nostdlib -e main -mcpu=cortex-m0plus
With our makefile ready the only thing we need to type to compile our program is make.
$ make
arm-none-eabi-gcc -c driver/sum.c -o Build/sum.o -mcpu=cortex-m0plus -I driver
arm-none-eabi-gcc -c main.c -o Build/main.o -mcpu=cortex-m0plus -I driver
arm-none-eabi-gcc Build/main.o Build/sum.o -o Build/main.elf -nostdlib -e main -mcpu=cortex-m0plus
How cool was that!!!. now it is easier to add more compilation options to our program, for instance activate the warnings ( -Wall ) indicate the compilation optimization level ( -O0, -O1, -O2, -Os ) or if we want debug symbols ( -g3 ), optimize memory ( -fdata-sections ) and a long etc..
all:
arm-none-eabi-gcc -c driver/sum.c -o Build/sum.o -mcpu=cortex-m0plus -I driver -Wall -O1 -g3 -fdata-sections
arm-none-eabi-gcc -c main.c -o Build/main.o -mcpu=cortex-m0plus -I driver -Wall -O1 -g3 -fdata-sections
arm-none-eabi-gcc Build/main.o Build/sum.o -o Build/main.elf -nostdlib -e main -mcpu=cortex-m0plus
A benefit of using makefiles is that we can create variables to place compilation options, here it is a example
GCC_FLAGS = -Wall -O1 -g3 -fdata-sections
all:
arm-none-eabi-gcc -c driver/sum.c -o Build/sum.o -mcpu=cortex-m0plus -I driver $(GCC_FLAGS)
arm-none-eabi-gcc -c main.c -o Build/main.o -mcpu=cortex-m0plus -I driver $(GCC_FLAGS)
arm-none-eabi-gcc Build/main.o Build/sum.o -o Build/main.elf -nostdlib -e main -mcpu=cortex-m0plus
Do not be shy, you can use as many variable as you need and add more compilation and link options and stuffs
# compiler options
GCC_FLAGS = -Wall -O1 -g -fdata-sections
# linker options
LD_FLAGS = -nostdlib -e main
# processor in use
CPU = -mcpu=cortex-m0plus -mthumb
# directories with header files
H_DIRS = -I driver
# Build instructions
all:
arm-none-eabi-gcc -c driver/sum.c -o Build/sum.o $(CPU) $(H_DIRS) $(GCC_FLAGS)
arm-none-eabi-gcc -c main.c -o Build/main.o $(CPU) $(H_DIRS) $(GCC_FLAGS)
arm-none-eabi-gcc Build/main.o Build/sum.o -o Build/main.elf $(CPU) $(LD_FLAGS)
There are more things to do with makefiles, but for now i think this is more than enough.