gccldautotoolsldflags

How to tell what gcc/autotools options from CFLAGS also go in LDFLAGS?


I have some -f compiler options in CFLAGS like -fsanitize=address and someone suggested to me a while ago that I also put those options in LDFLAGS so I did. It hasn't caused any problems.

My question is how can I tell what compiler options from CFLAGS also should go in LDFLAGS? Is it just the -f prefixed options? Thanks


Solution

  • I have some -f compiler options in CFLAGS like -fsanitize=address and someone suggested to me a while ago that I also put those options in LDFLAGS so I did. It hasn't caused any problems.

    Not only it won't cause any problems but that's the only correct way to build sanitized program.

    My question is how can I tell what compiler options from CFLAGS also should go in LDFLAGS? Is it just the -f prefixed options?

    It won't hurt if you simply do

    LDFLAGS = $(CFLAGS) $(LIBS)
    

    Compiler driver (/usr/bin/gcc) knows which high-level options are for the linker so unnecessary options will be simply filtered out. In case you wonder how this happens, run gcc with -dumpspecs flag:

    $ gcc -dumpspecs
    ...
    *link_command:
    %{!fsyntax-only:%{!c:%{!M:%{!MM:%{!E:%{!S:    %(linker) %{!fno-use-linker-plugin:%{!fno-lto:     -plugin %(linker_plugin_file)     -plugin-opt=%(lto_wrapper)     -plugin-opt=-fresolution=%u.res     %{!nostdlib:%{!nodefaultlibs:%:pass-through-libs(%(link_gcc_c_sequence))}}     }}%{flto|flto=*:%<fcompare-debug*}     %{flto} %{fno-lto} %{flto=*} %l %{no-pie:} %{pie:-pie} ...
    

    This looks a bit ugly but in reality that's just a mini-language for flag translation. Syntax details are given here (although I seriously doubt that you need need to know about them).