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

1

11

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.

Sennight answered 12/10, 2013 at 18:53 Comment(5)
I would simply code a shell wrapper which sets appropriately the LD_LIBRARY_PATH before exec-ing the binary ELF executable. This is common practice (most distributions are doing this for firefox)Academician
hi Basile thanks for your response right. I'm trying to write a shell wrapper as you said , But the problem is i need to run the script as . filename.sh (in the terminal) then only the export path works. if I try to start my application at system start up ,I'm not able to achieve the same .Sennight
No, just chmod a+x filename.sh, put filename.sh inside some directory in your PATH and you can run it simply as filename.shAcademician
I'm starting my application at system start up with application.sh which executes the filename.sh(shell script to set path) but it dosen't work ,the path is not setSennight
Then use a full path like e.g. $HOME/bin/filename.sh or /usr/local/bin/filename.sh. Look inside firefox or mozilla ; it is generally a shell script....Academician
H
16

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.

Hargis answered 12/10, 2013 at 19:20 Comment(2)
Changing LD_LIBRARY_PATH has no effect, except if the program calls after dlopen to load a plugin.Academician
Agreed. Any child processes (of the original process) would inherit a change in LD_LIBRARY_PATH made within that original process. So, setting LD_LIBRARY_PATH within your program, then forking and killing the parent should put you where (I presume) you want to be in terms of the load path.Hargis

© 2022 - 2024 — McMap. All rights reserved.