当我应该使用ld而不是gcc? [英] When i should use ld instead of gcc?

查看:119
本文介绍了当我应该使用ld而不是gcc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么时候应该使用ld链接器而不是gcc。我只是用c ++编写了一个简单的hello世界,当然我还包含了iostream库。如果我想用gcc创建一个二进制文件,我只需要使用:

g ++ hello hello.cpp
我已经得到了我的二进制文件。



后来我尝试使用ld连接器。要获得我使用的目标文件:
g ++ -c hello.cpp 。好吧,这很简单,但链接命令是可怕的长:



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

我知道 gcc 使用ld的事实。
在所有情况下或在大多数情况下,使用 gcc 会更好吗?请告诉我一些关于ld链接器有优势的案例。

解决方案

正如你所提到的,gcc只是在链接时作为ld的前端;它会传递所有链接器指令(选项,默认/系统库等),并通过为您处理所有这些特定于工具链的细节来确保所有内容都很好地融合在一起。



我认为最好将GNU工具链作为一个整体考虑在内,紧密集成的环境中(因为任何具有为某些外来嵌入式平台构建工具链的经验的人,比如dietlibc集成可能会同意)。除非你有一些非常具体的平台集成要求,或者有理由不使用gcc,否则我很难想到调用的好处。 ld 直接进行链接。您可能需要的任何额外的链接程序特定的选项都可以通过gcc命令行中的 -Wl,前缀轻松指定(如果尚未作为简单的gcc选项提供) / p>

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.

解决方案

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

这篇关于当我应该使用ld而不是gcc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆