Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dilemma about shared libraries on Unix

If I build a shared library (shared object) I can use it in two following ways:
First way is to use a shared library like I would use a static library.

           #include "myLib.h"
           //...
           //afterwards I can use functions defined in mylib.h
           myFunction();

The second way of using shared library is by calling dynamic loader API functions: dlopen, dlsym, and dlclose from dlfcn.h. I would use shared library in this way when I want to implement a plugin pattern, for example. Listing would look like this:

#include <dlfcn.h>

void  *myLib;                /*  Handle to shared lib file  */
void (*myFunction)();       /*  Pointer to loaded function  */

  //...

  //load shared object
  myLib = dlopen("/home/dlTest/myLib.so",RTLD_LAZY);
  dlerror();

  //get handle to function
  myFunction = dlsym( myLib, "myFunction");
  dlerror();

  //execute function
  (*myFunction)();

  //close lib
  dlclose(myLib);
  dlerror();

Now my first question is: what is the difference between these two usages of shared object in terms of loading time? By using shared library in first way, we're linking/loading shared library to main app in load-time and in the second way we're doing the same thing in run-time?

Second question. What is the name of these two usages? First one is called statically linked shared library and the second one is dynamically linked/loaded shared library?

Third question If I've built a shared library without -fPIC flag (osition independent code), would I be able to use it in a second way?

Cheers

like image 866
dragan.stepanovic Avatar asked Dec 10 '10 17:12

dragan.stepanovic


People also ask

What are shared libraries in UNIX?

Shared libraries are programs that, when loaded, are put in the shared library region for system-wide sharing. A program is loaded as a shared library program if the executable file has the shared library extended attribute set.

Why does Linux use shared libraries?

Shared libraries are the most common way to manage dependencies on Linux systems. These shared resources are loaded into memory before the application starts, and when several processes require the same library, it will be loaded only once on the system. This feature saves on memory usage by the application.

Are shared libraries shared between processes?

In short, only code is shared among processes, not data.

What is the difference between shared and static library?

Static libraries take longer to execute, because loading into the memory happens every time while executing. While Shared libraries are faster because shared library code is already in the memory.


1 Answers

These two use modes are typically called implicit and explicit. As you correctly stated the difference in loading is that explicitly linked dynamic library is loaded when dlopen is executed and implicitly linked library is loaded at the time the application is loaded in memory. Each dlopen may take milliseconds to finish, unless library was already loaded, in which case it's very fast, so if you have very stringent latency requirements or need to do frequent loading/unloading then you may decide to make linking implicit or explicitly load the library on program start and don't unload it until it's no longer used.

like image 112
Gene Bushuyev Avatar answered Sep 27 '22 18:09

Gene Bushuyev