16

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 somethink about cases where ld linker has advantage.

eri
  • 3,133
  • 1
  • 23
  • 35
Ice
  • 1,783
  • 4
  • 26
  • 52
  • 1
    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. – Ross Ridge Apr 17 '16 at 19:28
  • Thanks, i was afraid about that i didn't notice any helpful usage of ld. Btw u can use g++ -v -o hello hello.cpp which libraries you need to link. So all functions about link files/liberries, i can set using gcc? – Ice Apr 17 '16 at 19:46
  • 1
    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`. – Ross Ridge Apr 17 '16 at 19:51

2 Answers2

17

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

xbug
  • 1,394
  • 1
  • 11
  • 18
  • 3
    In conclusion, ld gives you more control/knowlegde over the files that are linked, I understand correctly? – Ice Apr 17 '16 at 20:06
  • 2
    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,`. – xbug Apr 17 '16 at 20:13
6

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.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • It's `-wl,` like `gcc test.c -Wl,-verbose` to pass verbose to ld – SAMPro Apr 13 '22 at 07:59
  • 1
    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`. – Thomas Dickey Apr 13 '22 at 08:16