View Full Version : Setting the path to find shared libraries files during runtime

11-06-2006, 05:17 AM
Hi all,

I am trying to use shared libraries in Linux. I use dlopen to load a .so file, and it worked if I set LD_LIBRARY_PATH before running my program. However, I want to set the LD_LIBRARY_PATH using setenv during runtime, instead of setting it before running the probram. I use setenv to set the LD_LIBRARY_PATH, but that does not work, and dlopen returns an error that it cannot find the libXXXX.so file. Could anyone give me some hints to set the path to find shared library files during runtime?


jim mcnamara
11-06-2006, 06:31 AM
Some ways to dlopen to work:
1. an existing LD_LIBRARY_PATH that points to directories that contain your library
2. a link in the default library path (usually /usr/lib) that points to your library file
3. an explicit path or directory hard-coded into your source as part of the library name

11-06-2006, 07:32 PM
Thanks for your reply. I have tried your solutions, but I am afraid that they cannot change the path to the shared library files during runtime. I am trying to get a path from the user, and load the shared libary file from that path, but using setenv to set the LD_LIBRARY_PATH during runtime does not work. The LD_LIBRARY_PATH in environment is changed, but dlopen does not use the new LD_LIBRARY_PATH. Is it possible to change the LD_LIBRARY_PATH when the program is running?

jim mcnamara
11-07-2006, 06:41 AM
LD_PRELOAD creates library interposers - by default dlopen searches whatever is in the env variable LD_PRELOAD before it does anything else. LD_LIBRARY_PATH.

man ld explains what LD_PRELOAD does. It may suit your needs. You are supposed to change those variables BEFORE program execution. Here is am example of why: