0

I was told that the dynamic library is good because it can save RAM, it's only needed while the program is running.

Now I have a question: if a program can be executed, can I say that all of dynamic libraries that it needs have been there (there wouldn't be any dymanic librariy missing error)?

Saything that a program comes from a link of three .o files, and each .o file needs a dymanic library. If I remove one dynamic library, can I still make the program run?

As I know, we have two ways to load a dynamic library:

  1. Load a dynamic library when we link object files, for example, g++ a.cpp -ltest, here we link the dynamic library libtest.so to our program, if we remove the libtest.so, we would not be able to execute the program.
  2. Load a dynamic library with the system api function: dlopen. In this case, as my understanding, if the execution of the program doesn't touch the code of dlopen, we would not have any error.

If I'm correct, could I say if the execution of the program has no error, it means all of dynamic libraries it needs are definitely there?

Kusalananda
  • 333,661
Yves
  • 3,291

1 Answers1

1

This basically depends on whether the dynamic libraries are loaded on program startup (that would be the normal behavior with ld.so) or lazily when the code that needs them is first executed. In the first case, the program will terminate with an error very early. In the second case, it might not execute the code needing the dynamic library at all and just run successfully. One example of the second case would be an audio file processing program that loads audio codecs depending on the file type. If you don't have an mp3 codec installed, it might still work with other file types. (The comment by muru points to a question where this is discussed more deeply)

  • Does the second case mean that we have to use the function, such as dlopen? In other words, if there is no function, such as dlopen, is used in the code, can I say that it must be the first case? – Yves Apr 17 '18 at 05:42
  • Note that lazy loading is the normal behaviour of glibc, but not that of musl. – muru Apr 17 '18 at 06:03