Makefiles
It is often useful to be able to build a project for the native desktop machine and also cross compile it for running on an embedded ARM device. For our internal Bluewater Systems projects we use a Makefile setup similar to the following:
ifeq "$(ARCH)" "arm"
CROSS=arm-none-linux-gnueabi-
else
ARCH=i686
CROSS=
endif
CC=$(CROSS)gcc
LD=$(CROSS)ld
ODIR = obj/$(ARCH)
ODIRDEP = $(ODIR)/.dir
PROG=hello_world
HEADERS=hello_world.h
SOURCES=hello_world.c
OBJECTS=$(patsubst %.c, $(ODIR)/%.o, $(SOURCES))
default: $(ODIR)/$(PROG)
$(ODIRDEP):
@echo " MKDIR $(ODIR)"
@[ -d $(ODIR) ] || mkdir -p $(ODIR)
@[ -f $(ODIRDEP) ] || touch $(ODIRDEP)
$(ODIR)/$(PROG): $(OBJECTS)
@echo " LD $@"
@$(CC) -o $@ $^ $(LFLAGS)
$(ODIR)/%.o: %.c $(ODIRDEP) $(HEADERS)
@[ -d $(ODIR) ] || mkdir -p $(ODIR)
@echo " CC $<"
@$(CC) -c -o $@ $< $(CFLAGS)
clean:
rm -Rf $(ODIR)
The important variables are:
- PROG: Name of the executable to build
- SOURCES: List of C source files
- HEADERS: List of header files
Running "make" with no arguments will build a native (i686) version of the application. The object files and the binary executable will be generated in the obj/i686/ directory under your projects working directory.
To build the ARM version of the binary, type:
make ARCH=arm
This will generate the ARM version of the object files and binary in the directory obj/arm/ under the project working directory.



