简体   繁体   中英

C executable code independence from shared libraries

I'm reading a book about gcc and the following paragraph puzzles me now:

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? 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

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

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. 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. Applications using the libraries (a .DLL for example) bind to the interface (meaning they call functions from the 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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