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

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

问题描述

我想知道什么时候应该使用 ld 链接器而不是 gcc.我只是用 C++ 写了一个简单的 hello world,当然我包括 iostream 库.如果我想用 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我有我的二进制文件.

后来我尝试使用 ld 链接器.要获取目标文件,我使用:g++ -c hello.cpp.好的,这很容易,但是链接命令太长了:

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 

我知道 gcc 使用 ld.gcc 在所有情况下或仅在大多数情况下都更好?请告诉我一些关于 ld 链接器具有优势的情况.

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.

推荐答案

正如你所说,gcc 只是在链接时充当 ld 的前端;它传递所有链接器指令(选项、默认/系统库等),并通过为您处理所有这些工具链特定细节来确保一切都很好地结合在一起.

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.

我认为最好将 GNU 工具链视为一个完整的、紧密集成的环境(任何有为某些特殊嵌入式平台构建工具链经验的人都可能同意,例如,dietlibc 集成).

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

除非您有一些非常具体的平台集成要求,或者有理由不使用 gcc,否则我很难想到直接调用 ld 进行链接有什么好处.您可能需要的任何额外的链接器特定选项都可以通过 gcc 命令行上的 -Wl, 前缀轻松指定(如果尚未作为普通 gcc 选项提供).

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天全站免登陆