Re: What options should be set in Make variables CFLAGS, CXXFLAGS, CPPFLAGS and LDFLAGS?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 2019-01-23 at 13:02 -0600, Peng Yu wrote:
> In Option Summary of the man page of gcc, I see many sections about
> options, like the following. What should be in CFLAGS, CXXFLAGS,
> CPPFLAGS and LDFLAGS, respectively?
> 
> For example, should -g be used in CPPFLAGS or both CFLAGS and
> CXXFLAGS? Should it be used with LDFLAGS? What about -O2? Thanks.

You probably should consider asking this on help-make@xxxxxxx instead.

Anyway.

CPPFLAGS are for preprocessor options, only.  So, -I, -D, -U, etc.  You
should not put things like -g there.  The idea behind separating this
is that you may want to run tools that invoke the preprocessor, such as
lint or other tools, but won't understand compiler flags.

Usually in makefiles linking is done with the compiler front-end, not
ld (the linker) directly.  And usually the CFLAGS or CXXFLAGS flags are
added to the link recipe.  So, you don't need to add them into LDFLAGS.
LDFLAGS should be for link-time only options such as -L, -Wl,..., etc. 
But do NOT put -l (library) options here, they go into LDLIBS.

Of course you can create other variables and use them, like:

  DEBUG = -ggdb3 -Og

  CFLAGS = $(DEBUG) ...
  CXXFLAGS = $(DEBUG) ...

Finally, if you're creating a makefile for use by people who might want
to customize your build you shouldn't be putting any but the most basic
flags in the CFLAGS or CXXFLAGS variables, so that people can override
them on the make command line without breaking the build:

  CFLAGS = -g -O2

  OTHER_CFLAGS = -Wall -Werror ...

then write your rule to use both $(CFLAGS) and $(OTHER_CFLAGS), so
people can run "make CFLAGS=-g" or something and things still work.




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux