I'll explain. File GNUmakefile:
First of all, GNU make looks for a make file called GNUmakefile, then Makefile, then makefile. I use GNUmakefile because the make file is specifically a GNU makefile, rather than a generic one.
ARCH ?= $(shell uname -m)
Specify a defailt for ARCH, the result of
uname -m
CXX_FILES := hello.cpp
List of C++ source files.
O_FILES = $(patsubst %.cpp,$(ARCH)/%.o,$(CXX_FILES))
Generate a list of object files in directory ARCH.
TARGET = $(addprefix $(ARCH)/,greeting)
Generate a target file in directory ARCH.
CXXFLAGS = -DARCH=\"$(ARCH)\"
Pass ARCH in quotes to the CPP files at compilation.
.PHONY: PRE
We need a dummy step to run first, call it PRE. It doesn't create a program, it just a place holder, a phony target.
all: PRE $(TARGET)
The first target, all. It depends on PRE and TARGET.
1 2
|
PRE:
$(shell test -d $(ARCH) || mkdir -p $(ARCH))
|
Before we begin, create the output directory.
1 2
|
$(ARCH)/%.o: %.cpp
$(COMPILE.cc) $(OUTPUT_OPTION) $<
|
We need to tell the build how to make a $(ARCH)/*.o file. It's made from a *.cpp file using the default rules.
1 2
|
$(TARGET): $(O_FILES)
$(LINK.cc) -o $@ $^
|
And this is how we link, default rules again.