Suppress make rule error output

Another way to suppress the make: error … (ignored) output is to append || true to a command that could fail. Example with grep that checks for errors in a LaTeX log file: undefined: @grep -i undefined *.log || true Without the || true, make complains when grep fails to find any matches. This works … Read more

Wildcard targets in a Makefile

The concept is called pattern rules. You can read about it in GNU make manual. $(GRAPHDIR)/%.png: $(GRAPHDIR)/%.dot dot $< -Tpng -o $@ graphs: $(patsubst %,$(GRAPHDIR)/%.png, Complex Simple IFileReader McCabe)\ or just %.png: %.dot dot $< -Tpng -o $@ graphs: $(patsubst %,$(GRAPHDIR)/%.png, Complex Simple IFileReader McCabe) You can also remove all repetition by extracting one of … Read more

Makefile to put object files from source files different directories into a single, separate directory?

There’s more than one way to do it, but this is a pretty good one (I really should have that hotkeyed). vpath %.cpp ../src src = Foo.cpp Bar.cpp test_src = Main.cpp FooTest.cpp BarTest.cpp objects = $(patsubst %.cpp,obj/%.o,$(src)) test_objects = $(patsubst %.cpp,obj/%.o,$(test_src)) $(objects): | obj obj: @mkdir -p $@ obj/%.o : %.cpp @echo $< @$(CXX) $(CXXFLAGS) … Read more

Run make in each subdirectory

There are various problems with doing the sub-make inside a for loop in a single recipe. The best way to do multiple subdirectories is like this: SUBDIRS := $(wildcard */.) all: $(SUBDIRS) $(SUBDIRS): $(MAKE) -C $@ .PHONY: all $(SUBDIRS) (Just to point out this is GNU make specific; you didn’t mention any restrictions on the … Read more

How can I configure my makefile for debug and release builds?

You can use Target-specific Variable Values. Example: CXXFLAGS = -g3 -gdwarf2 CCFLAGS = -g3 -gdwarf2 all: executable debug: CXXFLAGS += -DDEBUG -g debug: CCFLAGS += -DDEBUG -g debug: executable executable: CommandParser.tab.o CommandParser.yy.o Command.o $(CXX) -o output CommandParser.yy.o CommandParser.tab.o Command.o -lfl CommandParser.yy.o: CommandParser.l flex -o CommandParser.yy.c CommandParser.l $(CC) -c CommandParser.yy.c Remember to use $(CXX) or $(CC) … Read more