Let's say I changed a shared library and recompiled it.
Do I have to relink all main applications that use that shared lib (in case I'm using that shared library with implicit linking - #include "myLib.h"
) or relinking is done automatically when those applications load?
Asked
Active
Viewed 2,125 times
2

dragan.stepanovic
- 2,955
- 8
- 37
- 66
2 Answers
1
You should not have to relink. Shared libraries are linked automatically when the program starts. Do not think of it as "re"-linking, they are not physically linked in the first place.

Roman Zenka
- 3,514
- 3
- 31
- 36
-
when I'm initially building main application, does the shared library, which I intend to use with implicit linking, has to exist? In this case, what would the command for compiling and linking the main application that uses shared library look like? `g++ myprog.cpp -o myapp -lmySharedLib.so -ldl` ? Thanks – dragan.stepanovic Dec 11 '10 at 18:00
-
You should link only using the core name of the library. E.g. your `-ldl` actually links to `libdl.so` (and if not found `libdl.a`). So do `g++ myprog.cpp -o myapp -lmySharedLib -ldl` – Roman Zenka Dec 11 '10 at 18:24
-
The linker needs the library to be specified. The executable needs to be set up to load shared libraries when it starts. Also, the linker can not report missing symbols without access to the library. – Roman Zenka Dec 11 '10 at 19:31
1
You have to recompile only if the Application Binary Interface (ABI) of the library is incompatible with previous version - it could happen if some public functions are removed or their signature is changed, if some structures' sizes are changed, virtual functions order is changed and some other cases.
Otherwise you don't need re-linking - that is one of the main advantages of using shared libraries, allowing to distribute new versions of them without distributing new versions of the applications.

Dmitry Yudakov
- 15,364
- 4
- 49
- 53
-
Must the shared library exist (if I intend to use it with implicit linking), when I link the main app, or I just have to know it's name? For example in `g++ myprog.cpp -o myapp -lmySharedLib` here I'm linking `myapp` to `libmySharedLib.so`, but must that shared lib exist when linking `myapp` or it's enough for the linker to just know it's name and later, when `myapp` loads it searches for shared lib to resolved any unresolved references? Thanks for the help. – dragan.stepanovic Dec 12 '10 at 15:48
-
@kobac I think it has to exist, the linker still does some checking even though the code of the shared library is not linked into the application binary. – Dmitry Yudakov Dec 12 '10 at 18:09
-
If your ABI becomes incompatible, you should change the name of the library - otherwise everything breaks. You can add functions and items to the end of structs you allocate yourself without breaking the compatibility. – Roman Zenka Dec 13 '10 at 13:41