c语言中的链接和加载之间的区别是什么 [英] what is the difference between linking and loading in c language

查看:158
本文介绍了c语言中的链接和加载之间的区别是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

动态库的链接和加载是否都在运行时发生?
或者是只有在运行时才加载库?

Does linking and loading of the the dynamic libraries both happen at runtime? or is it that only loading of the library happens at run time?

推荐答案

静态链接和动态链接之间的区别。假设你指的是动态链接,那么:

See the earlier very good point about the distinction between static linking and dynamic linking. Assuming you are referring to the dynamic linking, then:

加载和(动态)链接都是由链接器完成的 - 在linux和其他Unix系统上, /lib/ld.so ,这是几乎在所有情况下由操作系统启动的实际程序。 ld.so 会将您的应用程式 - mygameBinary 依次载入 $ c> ld.so 然后从文件 mygameBinary 读取所需的动态链接库列表。

Both loading and (dynamic) linking are done by the linker – on linux and other Unix-alikes this is done by /lib/ld.so, which is the actual program that is launched by the operating system in almost all cases. ld.so in turn loads your application - mygameBinary into memory, and ld.so then reads from the file mygameBinary the list of dynamic linked libraries that it requires.

然后,链接器 ld.so 会依次将这些库加载到内存中。 libc.so libpthread.so libopengl.so ,并查看这些可能需要的其他图书馆,例如

The linker, ld.so, then loads each of these libraries into memory in turn, e.g. libc.so, libpthread.so, libopengl.so, and looks at what other libraries these might require, e.g. libm.so.

加载后, / strong>开始,查看由一个库或应用程序导出的命名对象或函数,以及由另一个库或应用程序导入的 的过程。链接器然后改变各种引用,并且有时候代码更新每个库中的非链接数据指针和函数调用,以指向实际数据或函数驻留的位置。例如,在 mygameBinary 中对 printf 的调用开始不指向任何东西(实际上它只是调用链接器),但是链接后跳转到 libc 中的 printf 函数。

Once loading is done, then linking begins, a process of looking at named objects or functions which are exported by one library or the application, and imported by another library or application. The linker then changes various references and sometimes code to update unlinked data pointers and function calls in each library to point where the actual data or function resides. For example, a call to printf in mygameBinary starts off pointing at nothing (actually it just calls the linker), but after linking becomes a jump to the printf function in libc.

链接完成后,通过调用 mygameBinary 中的 _start 函数启动应用程序

Once this linking is complete, the application is started, by invoking the _start function in mygameBinary, which then calls main, and your game starts.

以这种方式进行动态链接必须支持以下操作:

Dynamic linking in this way is necessary to support the following:


  • 应用程序发布后库更新在不同版本的操作系统上运行

  • 不确定库或应用程序在内存中的位置。

  • 通过共享物理ram由多个应用程序之间的库使用。

某些操作系统在细节上有所不同,例如OSX和AIX都会将一组特定的库预装入内存中的固定位置。这意味着他们不需要加载,只是链接,这可能更快。

Some OSs differ in the details, for instance OSX and AIX both pre-load a certain set of libraries into fixed locations in memory. This means they don't need to be loaded, just linked, which may be faster.

一些操作系统,如OSX,有时Linux支持预链接,这是一个过程,其中脚本在启动它们之前在系统上的应用程序上运行,并进行链接。当您启动它们时,您不需要链接它们。这一点很重要,因为在您启动应用程序时,链接需要大量的计算机时间,某些应用程序可能会每秒钟多次启动,例如 gcc ,<$ c在应用程序构建过程中创建$ c> cpp 作为,或者在索引您的计算机数据(OSX Spotlight)时过滤脚本。

Some OSs such as OSX and sometimes Linux support pre-linking, which is a process where a script runs over the applications on your system before you launch them, and does the linking. When you launch them, you then don't need to link them. This is important because linking takes a considerable amount of your computer's time when you launch an app, and some apps may be launched multiple times a second, such as gcc, cpp and as during application build process, or filter scripts when indexing your computer's data (OSX Spotlight).

这篇关于c语言中的链接和加载之间的区别是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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