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, Jan 23, 2019 at 2:05 PM Paul Smith <psmith@xxxxxxx> wrote:
>
> 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.

So only the following in the gcc manual go to CPPFLAGS. All other
options go to other GNU Make variables.

       Preprocessor Options
           -Aquestion=answer -A-question[=answer] -C  -dD  -dI  -dM  -dN
           -Dmacro[=defn]  -E  -H -idirafter dir -include file  -imacros file
           -iprefix file  -iwithprefix dir -iwithprefixbefore dir  -isystem
           dir -imultilib dir -isysroot dir -M  -MM  -MF  -MG  -MP  -MQ  -MT
           -nostdinc -P  -fdebug-cpp -ftrack-macro-expansion
           -fworking-directory -remap -trigraphs  -undef  -Umacro -Wp,option
           -Xpreprocessor option -no-integrated-cpp

> 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.

Do these belong to CFLAGS and CXXFLAGS instead of LDFLAGS? Thanks.

       Code Generation Options
           -fcall-saved-reg  -fcall-used-reg -ffixed-reg  -fexceptions
           -fnon-call-exceptions  -fdelete-dead-exceptions  -funwind-tables
           -fasynchronous-unwind-tables -fno-gnu-unique
           -finhibit-size-directive  -finstrument-functions
           -finstrument-functions-exclude-function-list=sym,sym,...
           -finstrument-functions-exclude-file-list=file,file,...  -fno-common
           -fno-ident -fpcc-struct-return  -fpic  -fPIC -fpie -fPIE
           -fno-jump-tables -frecord-gcc-switches -freg-struct-return
           -fshort-enums -fshort-double  -fshort-wchar -fverbose-asm
           -fpack-struct[=n]  -fstack-check -fstack-limit-register=reg
           -fstack-limit-symbol=sym -fno-stack-limit -fsplit-stack
           -fleading-underscore  -ftls-model=model -fstack-reuse=reuse_level
           -ftrapv  -fwrapv  -fbounds-check
           -fvisibility=[default|internal|hidden|protected]
           -fstrict-volatile-bitfields -fsync-libcalls

> 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.
>


-- 
Regards,
Peng



[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