"gcc链接"与"gcc链接"之间有什么关系?和"ld链接"? [英] What's the relationship between "gcc linking" and "ld linking"?

查看:174
本文介绍了"gcc链接"与"gcc链接"之间有什么关系?和"ld链接"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据说linux加载器是/usr/bin/ld,但是通常我们使用gcc/g ++来链接库和可执行文件,我们很少使用"ld".

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

我上次手动使用"ld"是在学习linux程序集时,生成可执行文件的唯一方法是ld .o文件以直接生成可执行文件而无需任何库.

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.

我的问题是,gcc/g ++是否包含某些"ld"的函数包装,因为原始的"ld"太难使用了吗?还是因为blablabla,我们永远不应该明确地将"ld"用于c/c ++程序链接?

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?

非常感谢.

推荐答案

gcc为ld提供了一些默认选项.

gcc supplies a few default options to ld.

ld对C ++或任何其他语言一无所知. ld不知道您的代码需要链接到哪些库.如果您尝试直接将编译后的C ++代码与ld链接,则它会对您有所帮助,因为ld本身不知道在哪里可以找到gcc的C ++运行时库libstdc++.你用琴弦吗?向量?其中大部分是模板代码,这些模板代码作为对象模块的一部分进行编译.但是libstdc++中仍然需要链接一些预编译的位.

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.

将编译后的代码提供给gcc进行链接时,gcc会很有礼貌地将所有文件传递给ld,并告知ld哪些库,以及您明确指定的任何库.

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.

如果愿意,可以直接与ld链接,只要您指定相同的库并使用gcc链接选项即可.但是,为什么要这样做呢?只需使用gcc链接您的gcc编译的代码即可.

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 c ode.

这篇关于"gcc链接"与"gcc链接"之间有什么关系?和"ld链接"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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