When should I use ld instead of gcc?
Asked Answered
S

3

17

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.

Sack answered 17/4, 2016 at 18:27 Comment(3)
Using the linker directly only has an advantage pretty much only when you're not using GCC. Otherwise using gcc or g++ is going to save you a lot of typing and more importantly all the grief involved in figuring out exactly what you need to type and when.Quipu
Thanks, i was afraid about that i didn't notice any helpful usage of ld. Btw u can use <code>g++ -v -o hello hello.cpp </code> which libraries you need to link. So all functions about link files/liberries, i can set using gcc?Sack
The problem is that that list of object files, libraries and options will change depending on what GCC options you use, what OS you're using, what version of GCC and potentially other factors. Note that you can link an object file created with GCC by using a command like gcc -o hello hello.o.Quipu
M
18

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

Mount answered 17/4, 2016 at 19:42 Comment(2)
In conclusion, ld gives you more control/knowlegde over the files that are linked, I understand correctly?Sack
It's more a matter of gcc guaranteeing a nice, painless integration (see @RossRidge 's comment about the multiple factors involved). Any ld option you may need you can still pass through gcc with -Wl,.Mount
H
5

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.

Haddad answered 17/4, 2016 at 19:53 Comment(2)
It's -wl, like gcc test.c -Wl,-verbose to pass verbose to ldBasic
Comma , is a separator, used to separate the option from its value (as well as separating tokens in the option value). The option is -Wl.Haddad
F
0

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.

Fondue answered 17/2 at 20:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.