10

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.

ruakh
  • 175,680
  • 26
  • 273
  • 307
Thiyagarajan
  • 1,271
  • 1
  • 14
  • 19
  • 3
    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`) – Basile Starynkevitch Oct 12 '13 at 19:32
  • hi [Basile](http://stackoverflow.com/users/841108/basile-starynkevitch) 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 . – Thiyagarajan Oct 14 '13 at 07:38
  • No, just `chmod a+x filename.sh`, put `filename.sh` inside some directory in your `PATH` and you can run it simply as `filename.sh` – Basile Starynkevitch Oct 14 '13 at 07:40
  • 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 set – Thiyagarajan Oct 14 '13 at 07:43
  • 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.... – Basile Starynkevitch Oct 14 '13 at 07:46

1 Answers1

15

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.

Nildram
  • 361
  • 2
  • 4
  • 5
    Changing `LD_LIBRARY_PATH` has no effect, except if the program calls after `dlopen` to load a plugin. – Basile Starynkevitch Oct 12 '13 at 19:24
  • 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. – Nildram Oct 12 '13 at 19:31