Why I Started Using This Tool
Once I had the architecture for this Linux Embedded C project planned out (four tasks, one shared
lifecycle), I hit the first real practical wall: how do I actually build this thing for the board?
Typing a long gcc cross-compile command by hand every time I changed a file was already
getting tedious after just one source file, and I knew this project was about to grow to at least four.
I wanted a build step I could trust to do the same correct thing every time, without me needing to
remember every flag.
What It Does
The Makefile wraps the whole cross-compilation process behind two commands: make and
make clean. It points at the Linaro ARM cross-compiler toolchain so the output binary runs
on the board's ARM processor instead of my Windows PC, bakes in -Wall -Wextra so the
compiler catches common bugs automatically, and adds -g -O0 so the resulting binary is
fully debuggable.
It uses Make's wildcard function to automatically pick up every .c file in the
src folder, so as this project grows to four task files (and beyond), I never have to go
back and edit the Makefile itself. I just drop in a new file and run make.
Here's the general shape of it:
# Linaro ARM cross-compiler
CC = arm-linux-gnueabihf-gcc
CFLAGS = -Wall -Wextra -g -O0
LDFLAGS = -lpthread
SRC = $(wildcard src/*.c)
OBJ = $(SRC:.c=.o)
TARGET = app
$(TARGET): $(OBJ)
$(CC) $(OBJ) -o $@ $(LDFLAGS)
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f $(OBJ) $(TARGET)
Each .c file compiles to its own .o file, so when I edit just the LED task,
make only recompiles that one file and relinks. Everything else is left alone.
Final Verdict
A Makefile feels like overkill for a one-file project, but the moment a project has more than one source file (especially a cross-compiled embedded one) it pays for itself immediately. This setup is dead simple, but it already solves the three things that actually matter: it always uses the right compiler, it always uses the right flags, and it only rebuilds what's actually changed.
If you're starting any embedded C project, I'd write your Makefile before your first real feature, not after. It's a five-minute investment that saves you from typing (and mistyping) the same long compile command a hundred times over.