This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


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@gnu.org 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 Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]