c++ - Linking a shared library in executable vs. another shared lib -


tl;dr

are there differences in way linux loads , links shared library if library linked executable or shared library?

background

suppose have shared library (e.g. liba.so) containing class static std::map , set of singleton classes. each singleton class has access map , statically adds instance of map.

there 2 scenarios:

  1. i use shared library (liba.so) in executable read registered classes global map.
  2. i use shared library (liba.so) in shared library (libb.so) , use new 1 in executable. in case libb.so uses map liba.so provide functionality executable (like facade).

problem

if use (i.e. link) shared library in executable (scenario 1), aforementioned map contains list of singleton classes, however, if use library in shared library , use new 1 in executable (scenario 2), map seems empty.

i cannot seem understand how linker handles shared libraries in either of cases.

update

as turned out libb.so not link liba.so correctly explicitly instructed using -la flag of g++. though cannot see liba.so linked libb.so using ldd, pmap or objdump, no runtime errors when using classes of liba.so. if run same command clang++ can see all required libraries listed.

i describe scenario produce behavior seeing.

  1. global constructors of singleton objects called, , registered against map.
  2. global constructor of map called, causing map initialized empty data structure.

another scenario:

  • if reading map libb.so global constructor, may called before object has had chance register itself.

generally, there no guaranteed order of execution of global constructors different translation units, , not different shared libraries.

a solution first issue above use singleton style pattern map, initialized on use rather through global constructor.

themap & globalmap () {     static themap instance;     return instance; } 

a solution second issue above suppress accessing global map global constructors. is, change global constructors in libb.so initialize on use rather initialize in global constructor.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -