Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why change in LD_LIBRARY_PATH at Runtime dosen't Reflect on the Executable once the Executable gets loaded

I'm trying to change the LD_LIBRARY_PATH from my C++ program. I'm able to get its value using getenv("LD_LIBRARY_PATH") and set its value using setenv() (and I know that this is working, because when I call getenv("LD_LIBRARY_PATH") again, I get the updated value), but changing its value from inside the program isn't having any effect on it: I still get this error-message:

Failed to Load the shared library file

If I set the value before the executable gets loaded or the application is started, it works fine.

like image 896
Thiyagarajan Avatar asked Oct 12 '13 18:10

Thiyagarajan


People also ask

Why do we need LD_LIBRARY_PATH?

In Linux, the environment variable LD_LIBRARY_PATH is a colon-separated set of directories where libraries should be searched for first, before the standard set of directories; this is useful when debugging a new library or using a nonstandard library for special purposes.

What is the use of LD_LIBRARY_PATH in Linux?

The LD_LIBRARY_PATH environment variable tells Linux applications, such as the JVM, where to find shared libraries when they are located in a different directory from the directory that is specified in the header section of the program.

How do I set LD_LIBRARY_PATH?

In your terminal, type the following sudo ldconfig and press enter on your keyboard. Close all your open terminals that you were using then open a new terminal session and run echo $LD_LIBRARY_PATH If you see the path you added is echoed back, you did it right.


1 Answers

Unfortunately setting LD_LIBRARY_PATH from within a running program will have no effect on it. The reason for this is that LD_LIBRARY_PATH is processed by the dynamic link loader (ld.so), which is the program which starts your program. Your program itself doesn't process LD_LIBRARY_PATH so changing it will have no effect.

like image 125
Nildram Avatar answered Sep 25 '22 13:09

Nildram