34

How can I get the command line GCC uses to invoke ld?

I have a problem for an AVR target where GCC apparently adds a linker option which I am trying to override, so I would like to look at the exact options GCC uses for ld.

1 Answers1

49

Use gcc -v to see what commands it runs. As in,

gcc -v -o hello hello.c

This will print a lot of output, including the linker command. The actual output depends on the platform, but the linking command should be near the end. Alternatively, use

gcc -### -o hello hello.c

This is like -v, but does not actually run any commands and quotes the options.

Another option is

gcc -dumpspecs

Look for the entry for link.

The above command line flags are listed in gcc --help and explained on the man page. Here's GCC documentation for the spec files.

Ville Laurikari
  • 28,380
  • 7
  • 60
  • 55
  • 3
    Or use gcc -### which is similar to -v but does not actually execute anything ant quotes all args – Laurynas Biveinis Jul 23 '09 at 15:27
  • 5
    Unfortunately as of GCC 7.3 this further hides the final linker (e.g. `/usr/bin/ld`) behind `collect2`: https://stackoverflow.com/questions/12584243/lets-analyse-collect2-ld-returned-1-exit-status – Ciro Santilli OurBigBook.com Nov 07 '18 at 07:24
  • 1
    What i found by trial and error is that collect2 also accepts the -v flag, so you can take the collect2 invocation from gcc -v, add a -v to it and run it to get the actual ld invocation. – plugwash Feb 10 '22 at 15:43