简体   繁体   English

C可执行代码独立于共享库

[英]C executable code independence from shared libraries

I'm reading a book about gcc and the following paragraph puzzles me now: 我正在读一本关于gcc的书,以下段落让我困惑:

Furthermore, shared libraries make it possible to update a library with- out recompiling the programs which use it (provided the interface to the library does not change). 此外,共享库可以更新库而无需重新编译使用它的程序(前提是库的接口不会更改)。

This only refers to the programs which are not yet linked, right? 这只是指尚未联系的节目,对吧? I mean, in C isn't executable code completely independent from the compiler? 我的意思是,在C中是不是可执行代码完全独立于编译器? In which case any alteration to the library, whether its interface or implementation is irrelevant to the executable code? 在哪种情况下,对库的任何更改,无论其接口或实现是否与可执行代码无关?

A shared library is not linked until the program is executed, so the library can be upgraded/changed without recompiling (nor relinking). 在执行程序之前,不会链接 共享库,因此可以升级/更改库而无需重新编译(也不需要重新链接)。

EG, on Linux, one might have EG,在Linux上,人们可能会有

    /bin/myprogram

depending upon 依赖于

    /usr/lib64/mylibrary.so

Replacing mylibrary.so with a different version (as long as the functions/symbols that it exports are the same/compatible) will affect myprogram the next time that myprogram is started. 用不同的版本替换mylibrary.so (只要它导出的函数/符号相同/兼容)将在下次启动myprogram时影响myprogram On Linux, this is handled by the system program /lib64/ld-linux-x864-64.so.2 or similar, which the system runs automatically when the program is started. 在Linux上,这由系统程序/lib64/ld-linux-x864-64.so.2或类似程序处理,系统在程序启动时自动运行。

Contrast with a static library, which is linked at compile-time. 静态库对比, 静态库在编译时链接。 Changes to static libraries require the application to be re-linked. 对静态库的更改需要重新链接应用程序。

As an added benefit, if two programs share the same shared library, the memory footprint can be smaller, as the kernel can “tell” that it's the same code, and not copy it into RAM twice. 另外一个好处是,如果两个程序共享同一个共享库,则内存占用量可以更小,因为内核可以“告诉”它是相同的代码,而不是将其复制到RAM中两次。 With static libraries, this is not the case. 对于静态库,情况并非如此。

No, this is talking about code that is linked. 不,这是在谈论一个链接代码。 If you link to a static libary, and change the library, the executable will not pick up the changes because it contains its own copy of the original version of the library. 如果链接到静态库并更改库,则可执行文件将不会获取更改,因为它包含自己的库原始版本副本。

If you link to a shared library, also known as dynamic linking , the executable does not contain a copy of the library. 如果链接到共享库(也称为动态链接) ,则可执行文件不包含库的副本。 When the program is run, it loads the current version of the library into memory. 程序运行时,会将当前版本的库加载到内存中。 This allows you to fix the library, and the fixes will be picked up by all users of the library without needing to be relinked. 这允许您修复库,修复程序将被库的所有用户选中,而无需重新链接。

Libraries provide interfaces (API) to the outside world. 库为外界提供接口(API)。 Applications using the libraries (a .DLL for example) bind to the interface (meaning they call functions from the API). 使用库的应用程序(例如.DLL)绑定到接口(意味着它们从API调用函数)。 The library author is free to modify the library and redistribute a newer version as long as they don't modify the interface. 只要不修改接口,库作者就可以自由地修改库并重新分发新版本。

If the library authors were to modify the interface, they could potentially break all of the applications that depend on that function! 如果库作者要修改接口,他们可能会破坏依赖于该功能的所有应用程序!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM