I understand that CFLAGS (or CXXFLAGS for C++) are for the compiler, whereas CPPFLAGS is used by the preprocessor.
But I still don't understand the difference.
I need to specify an include path for a header file that is included with #include — because #include is a preprocessor directive, is the preprocessor (CPPFLAGS) the only thing I care about?
Under what circumstances do I need to give the compiler an extra include path?
In general, if the preprocessor finds and includes needed header files, why does it ever need to be told about extra include directories? What use is CFLAGS at all?
(In my case, I actually found that BOTH of these allow me to compile my program, which adds to the confusion… I can use CFLAGS OR CPPFLAGS to accomplish my goal (in autoconf context at least). What gives?)
Best Answer
The implicit make rule for compiling a C program is
where the
$()
syntax expands the variables. As bothCPPFLAGS
andCFLAGS
are used in the compiler call, which you use to define include paths is a matter of personal taste. For instance iffoo.c
is a file in the current directorywill both call your compiler in exactly the same way, namely
The difference between the two comes into play when you have multiple languages which need the same include path, for instance if you have
bar.cpp
then trythen the compilations will be
as the C++ implicit rule also uses the
CPPFLAGS
variable.This difference gives you a good guide for which to use - if you want the flag to be used for all languages put it in
CPPFLAGS
, if it's for a specific language put it inCFLAGS
,CXXFLAGS
etc. Examples of the latter type include standard compliance or warning flags - you wouldn't want to pass-std=c99
to your C++ compiler!You might then end up with something like this in your makefile