Results 1 to 6 of 6
Hi,
hope someone can help me with my little problem.
In my C++ program I try to set LD_LIBRARY_PATH by using
setenv("LD_LIBRARY_PATH", (char*) strMyPath.c_str(), 1);
The library is then loaded ...
- 08-15-2007 #1Just Joined!
- Join Date
- Aug 2007
- Posts
- 3
Problem to change LD_LIBRARY_PATH with setenv()
Hi,
hope someone can help me with my little problem.
In my C++ program I try to set LD_LIBRARY_PATH by using
setenv("LD_LIBRARY_PATH", (char*) strMyPath.c_str(), 1);
The library is then loaded with:
handle = dlopen (strLibrary.c_str(), RTLD_NOW | RTLD_GLOBAL);
It does not work to load my library, since it does not know the path. It does not even work for the child.
When I set LD_LIBRARY_PATH using:
export LD_LIBRARY_PATH=Mypath (has got the same input as strMyPath) before starting my application, it works fine.
As it seems to me the LD_LIBRARY_PATH is loaded at program startup. Is there a way of setting LD_LIBRARY_PATH or another variable from my C++ application?
Thank you very much in advance.
Bye Kay
- 08-15-2007 #2Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Hi,
The answer is no.
You can't change this within a program to have effect on that program because the run-time linker is already loaded by the time your program starts.
It should be a potential security problem if a program should be able to change D_LIBRARY_PATH without the user's knowledge at runtime.
Regards
- 08-15-2007 #3Just Joined!
- Join Date
- Aug 2007
- Posts
- 3
Thanks
Thanks for the fast reply!
Any other ideas how I can specify an additional search path for dlopen() at runtime?
The only idea which comes to my mind is using a script, but this would complicate things too much.
Thanks!
- 08-15-2007 #4Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
I think the easiest way is to used a script, but I hope this can give you more ideas:
http://tldp.org/HOWTO/Program-Library-HOWTO/index.html
Regards
- 08-15-2007 #5Just Joined!
- Join Date
- Aug 2007
- Posts
- 3
Thanks
Thanks for your help, but I have solved the problem differently.
As you have pointed out, the LD_LIBRARY_PATH is only loaded at startup of a new process. During runtime any change is ignored!
That is why I have just called execve("path/app", ppArgs, ppEnv), where
char* ppArgs = {"app", (char*) 0};
char* ppEnv = {"LD_LIBRARY_PATH=PATH1:PATH2", (char*) 0};
This kind of way just works fine and is more or less the same solution, but it is using C/C++ to solve it
- 08-15-2007 #6Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Hi,
Nice solution, I'll make a note of that!
Thanks and best regards


Reply With Quote