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?)
The implicit make rule for compiling a C program is
where the
$()syntax expands the variables. As bothCPPFLAGSandCFLAGSare used in the compiler call, which you use to define include paths is a matter of personal taste. For instance iffoo.cis 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.cppthen trythen the compilations will be
as the C++ implicit rule also uses the
CPPFLAGSvariable.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,CXXFLAGSetc. Examples of the latter type include standard compliance or warning flags – you wouldn’t want to pass-std=c99to your C++ compiler!You might then end up with something like this in your makefile