Ice
Ice

Reputation: 1961

When should I use ld instead of gcc?

I want to know when I should use ld linker instead off gcc.

I just wrote a simply hello world in c++, of course I include iostream library. If I want make a binary file with gcc, I just use: g++ hello hello.cpp and I've got my binary file.

Later I try to use ld linker. To get object file I use: g++ -c hello.cpp. OK that was easy, but the link command was horrible long:

ld -o hello.out  hello.o \
   -L /usr/lib/gcc/x86_64-linux-gnu/4.8.4/ \
   /usr/lib/gcc/x86_64-linux-gnu/4.8.4/crtbegin.o \
   /usr/lib/gcc/x86_64-linux-gnu/4.8.4/crtend.o \
   /usr/lib/x86_64-linux-gnu/crti.o \
   /usr/lib/x86_64-linux-gnu/crtn.o \
   /usr/lib/x86_64-linux-gnu/crt1.o \
   -dynamic-linker /lib64/ld-linux-x86-64.so.2 -lstdc++ -lc 

I know fact that gcc uses the ld. Using gcc is better in all cases or just in most cases? Please, tell me something about cases where ld linker has advantage.

Upvotes: 19

Views: 17792

Answers (3)

emacs drives me nuts
emacs drives me nuts

Reputation: 3888

In almost all cases, use gcc as a linker driver

  • The gcc driver program does not just call ld but also collect2 to gather information required for a correct binary / startup code.
  • gcc knows how to use the linker and provides many options: Which startup-code to link, which libraries, which emulation, which multilib variant. Today, the linker is calling back the compiler by means of a plugin for LTO (link time optimizations) compilations with all the pass-through options so the LTO compiler (lto1) will work as desired. You do not want to do that by hand. A simple gcc link passes options worth more than 1200 characters to ld. And all depend on profiling (-p, -pg) relocatable link (-r) and whatnot. When you don't want to link a specific part, gcc has likely the right option for you: -nocrt0, -nostartfiles, -nostdlib, -nolibc, ...

When to call ld directly

As you asked: There are few and rare cases when you want to call ld directly.

  • You want to find the Binutils version, e.g. ld -v.

  • You want to find out which options the linker supports, e.g. ld --help. Then use these options to augment a gcc invocation with -Wl or -Xlinker, e.g. to generate a map file and to define a symbol:

    gcc ... -Wl,-Map,main.map -Wl,--defsym,my_symbol=0x42

  • You found a bug in ld and are preparing a bug report. Keep it as minimal and self-contained as possible, so that the Binutils people don't have to guess what magic your gcc is adding.

  • You want to find out about ld's capabilities, e.g. in a configure script: Which options does ld support, what features etc. For example in a GCC configure script you cannot call the linker via gcc because you are only just building the compiler (cc1, cc1plus, lto1) and gcc driver.

  • You are writing a test suite for the linker like the Binutils test suite.

Upvotes: 1

Thomas Dickey
Thomas Dickey

Reputation: 54455

It is mostly a matter of taste: you would use ld directly when the command-lines are simpler than using gcc. That would be when you are just using the linker to manipulate a small number of shared objects, e.g., to create a shared library with few dependencies.

Because you can pass options to ld via the -Wl option, often people will recommend just using gcc to manage the command-line.

Upvotes: 5

xbug
xbug

Reputation: 1462

As you mentioned, gcc merely acts as a front-end to ld at link time; it passes all the linker directives (options, default/system libraries, etc..), and makes sure everything fits together nicely by taking care of all these toolchain-specific details for you.

I believe it's best to consider the GNU toolchain as a whole, tightly integrated environment (as anyone with an experience of building toolchains for some exotic embedded platforms with, say, dietlibc integration will probably agree).

Unless you have some very specific platform integration requirements, or have reasons not to use gcc, I can hardly think of any advantage of invoking ld directly for linking. Any extra linker-specific option you may require could easily be specified with the -Wl, prefix on the gcc command line (if not already available as a plain gcc option).

Upvotes: 19

Related Questions