Home | Projects | Notes > ARM Cortex-M3/M4 Processor > Build Process & Makefile
[!] Note: The compiler does not save .i
and .s
file by default. You can make the compiler save them by using the compiler flags
Note that you need to specify some target-specific information when cross-compiling. These include the processor name or processor architecture, Thumb/ARM state, etc. Otherwise, the assembler may not understand some architecture-specific inline assembly code. (Check the "Machine-Dependent Options" section in the online GCC documentation.)
-c
: Compile or assemble the source files, but do not link
-S
: Stop after the stage of compilation proper; do not assemble.
-o
: Designate output file name
-mcpu=
: Specifies the name of the target ARM processor for which GCC should tune the performance of the code
-march=
: Specifies the name of the target ARM architecture. GCC uses this name to determine what kind of instructions it can emit when generating assembly code. This option can be used in conjunction with or instead of the -mcpu=
option. (For the ARM Cortex M4 processor, -march=armv7ve
)
-mthum
: Thumb state ISA
-marm
: ARM state ISA (default)
For more information about compiler flags, consult GCC documentation.
xxxxxxxxxx
11arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb main.c -o main.o
Generates
main.o
xxxxxxxxxx
11arm-none-eabi-gcc -S -mcpu=cortex-m4 -mthumb main.c -o main.s
Generates
main.s
Make is a tool.
xxxxxxxxxx
21$ which make
2/usr/bin/make
xxxxxxxxxx
21$ make # correct; make is an executable
2$ makefile # incorrect; makefile is NOT an executable
Makefile is a file written in such a way that Make tool can understand.
Make tool needs Makefile as the input and process as per the instructions written in it.
"Makefile" or "makefile" both are valid notations.
Makefile is used for automating the build process.
Make tool is very powerful in that it can detect the modified source file(s) and process/build file them only by running make. Compiling only the modified source files and linking them into the project can save huge amount of time when only a small number of source files has been modified in a big project.
Time stamp is used to detect the modified source files.
In general, compilation is much slower process than linking. Make tool takes the advantage of this fact.
Makefile for "Task Scheduler" project:
xxxxxxxxxx
431# Makefile (or makefile)
2
3# Variables
4CC=arm-none-eabi-gcc
5MACH=cortex-m4
6CFLAGS= -c -mcpu=$(MACH) -mthumb -mfloat-abi=soft -std=gnu11 -Wall -o0
7LDFLAGS= -mcpu=$(MACH) -mthumb -mfloat-abi=soft --specs=nano.specs -T stm32_ls.ld -Wl,-Map=final.map
8# LDFLAGS for semihosting
9LDFLAGS_SH= -mcpu=$(MACH) -mthumb -mfloat-abi=soft --specs=rdimon.specs -T stm32_ls.ld -Wl,-Map=final.map
10
11all: main.o led.o stm32_startup.o syscalls.o final.elf
12
13# semihosting
14sh: main.o led.o stm32_startup.o final_sh.elf
15
16# Target (dependencies / recipie)
17main.o: main.c # target is 'main.o', and 'main.c'(dependency) is necessary to create target
18 $(CC) $(CFLAGS) -o $@ $^ # recipie: command to generate the target
19 # $(CC) $(CFLAGS) $^ -o $@
20 # $(CC) $(CFLAGS) main.c -o main.o
21 # '$^' represents dependency, '$@' represents target (@ does look like a target :))
22
23led.o: led.c
24 $(CC) $(CFLAGS) -o $@ $^
25
26stm32_startup.o: stm32_startup.c
27 $(CC) $(CFLAGS) -o $@ $^
28
29syscalls.o: syscalls.c
30 $(CC) $(CFLAGS) -o $@ $^
31
32final.elf: main.o led.o stm32_startup.o syscalls.o
33 $(CC) $(LDFLAGS) -o $@ $^
34
35final_sh.elf: main.o led.o stm32_startup.o # exclude 'syscalls.o'
36 $(CC) $(LDFLAGS_SH) -o $@ $^
37
38clean:
39 rm -rf *.o *.elf # in Windows rm -> del
40
41connect: # connect board with OpenOCD
42 openocd -f /board/stm32f4discovery.cfg
43 # /usr/share/openocd/scripts/board/stm32f4discovery.cfg
-std=
: Specifies the language standard. (Check "Options Controlling C Dialect" section of the GCC documentation)
-On
: Specifies the optimization level to n (by default 0)
Nayak, K. (2022). Embedded Systems Programming on ARM Cortex-M3/M4 Processor [Video file]. Retrieved from https://www.udemy.com/course/embedded-system-programming-on-arm-cortex-m3m4/