Imagine that there is a shared library S
, there are shared libraries A
and B
, and an executable C
. A
and B
are linked against S
. C
is not linked against A
and B
, instead C
dynamically loads A
and B
via dlopen
. S
has a static-storage-duration object O
with external linkage that is used inside A
and B
.
I know that the linker guaranties the uniqueness of static objects with external linkage, for example, if C
would be linked against A
and B
then O
will be unique, but I am not sure about the dynamic library loading.
Will the dynamically loaded libraries A
and B
share the same memory of object O
, or it will be two separately allocated objects O
? If the object O
is shared, then loading A
, modifying object O
, loading B
should work find, i.e. B
will see the changes to the object O
?
Advertisement
Answer
First, there’s no difference whether you let the program loader load the dynamically shared object or you do it explicitly, e.g. with dlopen()
on a *nix system. The same rules apply.
But in both cases, you will have the same object instance in your A
and B
. This is because with C
linking both of them, both are in the same process. The dynamic linker just ensures that every process gets its own copy of static objects (while still all processes might share the code segment).