2

I just wanted to understand how shared libraries with statically linked libraries are expected to perform. I am writing a shared library (lshared.so) that is statically linked to another library (lstatic). Now, I have another executable helloworld which loads a dynamic version of library lstatic (lstatic.so) and lshared.so as well. Will the static version of library (lstatic) loaded by lshared.so and the dynamic version (lstatic.so) conflict? Will they share any global state? Tried to show it better below.

helloworld
     --> lshared.so (statically linked to lstatic at compile time)
     --> lstatic.so (hello world loads this at runtime)

To throw some context, I've to use a shared library for logging. It is possible that lshared.so is statically linked to a different version that the one available to helloworld at runtime.

JonasVautherin
  • 7,297
  • 6
  • 49
  • 95
Prabhu
  • 3,434
  • 8
  • 40
  • 48
  • You are still using dynamic libraries. Static linking just caused that library is lazy loaded by your program without need of writing single line of code. Those dynamic libraries will be shared implicitly. – Marek R Jan 12 '22 at 18:03
  • Doesn't static linking implicitly mean that there is no loading at run time? I did not quite understand what you mean by lazy loading a static linked library. – Prabhu Jan 12 '22 at 18:23
  • you should show command lines which you used to create those libs and your executable. `.so` actually indicates a shared library. Also, run `ldd helloworld` and see which libraries it lists. – Serge Jan 12 '22 at 20:36
  • The symbols are [shared](https://stackoverflow.com/q/11005881/4074081) by default and loaded dynamically even across shared libraries. You need to compile with `-fvisibility=hidden` or apply [__attribute__ ((visibility ("hidden")))](https://gcc.gnu.org/wiki/Visibility) to the symbols you need to keep private or compile the shared library with [-Wl,--exclude-libs,ALL](https://stackoverflow.com/a/14863432/4074081) to automatically hide symbols from all linked static libraries. – dewaffled Jan 12 '22 at 21:04
  • 2
    No. Then library first have to be build as static to work like this. In your case you have shared library which was linked dynamically. So lib*.so file is loaded lazily behind scene. Here is nice explanation how this works: https://youtu.be/dOfucXtyEsU?t=2334 – Marek R Jan 12 '22 at 21:09

1 Answers1

1

Will the static version of library (lstatic) loaded by lshared.so and the dynamic version (lstatic.so) conflict.

Possibly.

Will they share any global state.

Possibly.

The answers depend on how exactly lshared.so is built (which symbols it exports), and how the main helloworld binary is linked.

The answers also tend to be somewhat complicated, and may depend on inlining and other optimizations, and possibly on compiler versions.

It is best to avoid doing that by using shared version of lstatic.so, where all the answers become simple.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • Say `lshared.so` does not export any symbol of `lstatic`. Does that make it easier? Or could there still be conflicts somehow? – JonasVautherin Sep 15 '22 at 23:05