Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting LD_LIBRARY_PATH environment variable for loading a shared library at runtime (g++)

I'm having two problems related to the same issue:

  1. I have a shared object saved in `pwd`/lib and while the executable that uses it compiles successfully (by using -l and -L switches), at runtime, it's giving me grief. If I try to run LD_LIBRARY_PATH=/my/absolute/path/to/library/directory ./test it works fine. But if I export LD_LIBRARY_PATH=/my/absolute/path/to/library/directory and do ./test it says that it can't find the shared library. However, if I do LD_LIBRARY_PATH=$LD_LIBRARY_PATH ./test again it works fine!! Any ideas on what I'm doing wrong?

  2. Second issue is related to the exporting of the LD_LIBRARY_PATH env variable. If I open a terminal and type export LD_LIBRARY_PATH=/path/to/stuff and then type echo $LD_LIBRARY_PATH, the variable is correct. However if I write a script containing the export command, simply running it doesn't update the variable, instead I need to run source install.sh in order to actually persist the variable. What's the best solution for this?

Thank you for your time!

like image 528
rhobincu Avatar asked Mar 31 '13 11:03

rhobincu


People also ask

What is LD_LIBRARY_PATH environment variable?

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.

What is the difference between path and LD_LIBRARY_PATH?

The PATH environment variable specifies the search paths for commands, while LD_LIBRARY_PATH specifies the search paths for shared libraries for the linker. The initial default values of PATH and LD_LIBRARY_PATH are specified in the buildfile before procnto is started.

What is Ld_preload and LD_LIBRARY_PATH?

LD_PRELOAD (not LD_PRELOAD_PATH ) is a list of specific libraries (files) to be loaded before any other libraries, whether the program wants it or not. LD_LIBRARY_PATH is a list of directories to search when loading libraries that would have been loaded anyway.


2 Answers

To answer the second question first:

source executes the script inside the current shell, ./install.sh opens and executes it in a different shell. http://www.unix.com/unix-dummies-questions-answers/537-difference-between-source-exec-script.html

Now for your first question:

LD_LIBRARY_PATH=$LD_LIBRARY_PATH ./test sets the LD_LIBRARY_PATH variable before just one command (the ./test command). For the same reason above, I believe this isn't getting transferred to whatever shell ./test creates. To make it persist, you may need to put the export LD_LIBRARY_PATH=... in your ~/.bashrc

like image 183
maditya Avatar answered Sep 20 '22 21:09

maditya


I have found sometimes adding -L explicitly via the CFLAGS environment variable is successful when LD_RUN_PATH was not. As in: export CFLAGS=-L/opt/tool/lib

like image 21
ckg Avatar answered Sep 20 '22 21:09

ckg