This is the mail archive of the
mailing list for the GCC project.
Re: What options should be set in Make variables CFLAGS, CXXFLAGS, CPPFLAGS and LDFLAGS?
- From: Paul Smith <psmith at gnu dot org>
- To: Peng Yu <pengyu dot ut at gmail dot com>, "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Wed, 23 Jan 2019 15:05:28 -0500
- Subject: Re: What options should be set in Make variables CFLAGS, CXXFLAGS, CPPFLAGS and LDFLAGS?
- References: <CABrM6wk33tv5HVaUOWYMy7ssAQD0CdLsdTX0=RnScHiJa-Yfirstname.lastname@example.org>
- Reply-to: psmith at gnu dot org
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 email@example.com instead.
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.