CPPFLAGS
seems to be an invention of GNU Make, referenced in some of its built-in recipes.
If your program is built by some Free software distributions, you may find that some of them require packages to interpolate this variable, using CPPFLAGS
for passing down options like -D_WHATEVER=1
for passing down a macro definition.
This separation is a poor idea and completely unnecessary in the GNU environment because:
There is a way to run gcc
to do preprocessing only (while ignoring compiler options unrelated to preprocessing).
The stand-alone GNU cpp
is tolerant to compiler options, such as -W
warnings that do not pertain to preprocessing and even code generation options like -fstrict-aliasing
and the linker-pass through like -Wl,--whatever
.
So generally speaking, build systems that need to call the stand-alone preprocessor for whatever reason can just pass it $(CFLAGS)
.
As an application developer writing a Makefile
, you cannot rely on the existence of CPPFLAGS
. Users who are not insider experts in open source building won't know about CPPFLAGS
, and will do things like make CFLAGS=-Dfoo=bar
when building your program. If that doesn't work, they will be annoyed.
As a distro maintainer, you cannot rely on programs to pull in CPPFLAGS
; even otherwise well-behaved ones that pull in CFLAGS
, LDFLAGS
and LDLIBS
.
It's easy enough for the application developers to write GNU Make code to separate preprocessor flags out of $(CFLAGS)
:
cpp_only_flags := $(foreach arg, \
$(CFLAGS), \
$(or $(filter -D%,$(arg)), \
$(filter -U%,$(arg)), \
$(filter -I%,$(arg)), \
$(filter -iquote%,$(arg)), \
$(filter -W%,$(arg)), \
$(filter -M%,$(arg)))) \
$(CPPFLAGS) # also pull this in
all:
@echo cpp_only_flags == $(cpp_only_flags)
Demo:
$ make CFLAGS="-Wall -I/path/to/include -W -UMAC -DFOO=bar -o foo.o -lm"
cpp_only_flags == -Wall -I/path/to/include -W -UMAC -DFOO=bar
In the case of the GNU compiler and preprocessor, this is probably unnnecessary; but it illustrates a technique that could be used for non-GNU compilers and preprocessors, in a build system based on GNU Make.