0

This question already has an answer here:

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?

marked as duplicate by muru, Kusalananda, Kiwy, Stephen Kitt, Christopher Apr 17 '18 at 16:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • ^Given that question, you obviously know a counter example to your own question. – muru Apr 17 '18 at 2:08
  • @muru Read my reedition please. – Yves Apr 17 '18 at 5:56
  • s/the execution/an execution/ - you cannot conclude that from a single execution for the reasons already given in Stephen Kitt's answer. – muru Apr 17 '18 at 6:04
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 5:42
  • Note that lazy loading is the normal behaviour of glibc, but not that of musl. – muru Apr 17 '18 at 6:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.