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

How do I perform arithmetic in a makefile?

Using Bash arithmetic expansion: SHELL=/bin/bash JPI=4 JPJ=2 all: echo $$(( $(JPI) * $(JPJ) )) The first line is to choose the Bash shell instead of the default (sh). Typically, sh doesn’t support arithmetic expansion. However in Ubuntu, /bin/sh is provided by Dash, which supports this feature. So that line could be skipped. The double dollar … 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

CMAKE_MAKE_PROGRAM not found

I have two suggestions: Do you have make in your %PATH% environment variable? On my system, I need to add %MINGW_DIR%\bin to %PATH%. Do you have make installed? Depending on your mingw installation, it can be a separate package. Last resort: Can you pass the full path to make on the commandline? cmake -D”CMAKE_MAKE_PROGRAM:PATH=C:/MinGW-32/bin/make.exe” ..\Source

CFLAGS vs CPPFLAGS

The implicit make rule for compiling a C program is %.o:%.c $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $< where the $() syntax expands the variables. As both CPPFLAGS and CFLAGS are used in the compiler call, which you use to define include paths is a matter of personal taste. For instance if foo.c is a … Read more

Makefile ifeq logical or

As found on the mailing list archive, http://osdir.com/ml/gnu.make.windows/2004-03/msg00063.html http://osdir.com/ml/gnu.make.general/2005-10/msg00064.html one can use the filter function. For example ifeq ($(GCC_MINOR),$(filter $(GCC_MINOR),4 5)) filter X, A B will return those of A,B that are equal to X. Note, while this is not relevant in the above example, this is a XOR operation. I.e. if you instead have … 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

What are Makefile.am and Makefile.in?

Makefile.am is a programmer-defined file and is used by automake to generate the Makefile.in file (the .am stands for automake). The configure script typically seen in source tarballs will use the Makefile.in to generate a Makefile. The configure script itself is generated from a programmer-defined file named either configure.ac or configure.in (deprecated). I prefer .ac … Read more