What's the relationship between "gcc linking" and "ld linking"?
Asked Answered
F

3

12

It's said that linux loader is /usr/bin/ld, but usually we use gcc/g++ to link libraries and executables, we barely use "ld".

The last time I used "ld" manually was when I was learning linux assembly, the only way to generate executable is to ld a .o file to generate executable directly without any library.

My question is, is gcc/g++ containing some function wrappers of "ld", because raw "ld" is too difficult to use? Or we should never use "ld" explicitly for c/c++ program linking, because of blablabla?

Foliated answered 29/12, 2016 at 1:47 Comment(1)
Also see When i should use ld instead of gcc?, Linking C++ code with 'gcc' (without g++), and friends.Hartsfield
W
13

gcc supplies a few default options to ld.

ld doesn't know anything about C++, or any other language. ld has no idea what libraries your code needs to link with. If you try to link your compiled C++ code with ld directly, it'll bail out on you, since ld, by itself, has no idea where it can find libstdc++, gcc's C++ runtime library. Do you use strings? vectors? Most of that is template code that gets compiled as part of your object module. But there are a still few precompiled bits, in libstdc++, that need to be linked with.

When you give your compiled code to gcc to link, gcc will be courteous enough to pass all your files along to ld, and tell ld which libraries, in addition to any ones you explicitly specify.

You can link with ld directly, if you want to, as long as you specify the same libraries and link option gcc uses. But why would you want to do that? Just use gcc to link your gcc-compiled code.

Wallacewallach answered 29/12, 2016 at 1:53 Comment(0)
B
5

You shouldn't attempt to directly use ld to link a C++ program because you need to know the implementation detail of where the static part of the C++ runtime library is located. g++ knows these implementation details, such as where to find the file libstdc++.a. If you tried to use ld directly, you would have to supply all these "missing" static libraries.

Billingsgate answered 29/12, 2016 at 1:55 Comment(1)
Do you mean, even when we default to use libstdc++.so, there's still some content inside libstdc++.a being linked statically into our executable? What are these content doing? Thanks.Foliated
S
3

My question is, is gcc/g++ containing some function wrappers of "ld"

That's right.

because raw "ld" is too difficult to use?

Well, not really; you could use it yourself without too much trouble, but it's convenient to manage the entire build process through a single executable, with a single suite of flags, and often with a single command.

It's also likely that you'd have to provide absolute paths to some runtime libraries (e.g. libstdc++.a) yourself if you bypassed the wrapper (though I haven't tested this).

Or we should never use "ld" explicitly for c/c++ program linking, because of blablabla?

You're free to do so if you want. The only reason people might raise their eyebrows is to ask why you're not doing it in the conventional manner. If you have a good reason to invoke ld directly, rather than going through g++ and passing through any linker flags that way, then go right ahead!

Scholasticism answered 29/12, 2016 at 1:54 Comment(3)
then how to do a partial linking with gcc?Rummer
@Rummer Sorry, what? If you have a new question, please post it as such with all necessary detail and context.Scholasticism
Apologise, it's just a response for the last sentence If you have a good reason to invoke ld directly, rather than going through g++ and passing through any linker flags that way, then go right ahead! . it seems when you want to some partial linking for example ld -r -o foobar.o foo.o bar.o , ld is the best choice.Rummer

© 2022 - 2024 — McMap. All rights reserved.