Thursday, June 9, 2011

Shared library cannot be found issue

This issue happens if the location of the shared library used when compiling your code is not the default search location of the OS. To solve this problem there are two easy ways:

A. Set environment variable $LD_RUN_PATH before compiling/linking the executables
The value of this environment variable should be the paths where your shared libraries are located, separated by colons if there are two or more directories. It will be used when compiling/linking so that the location information will be in the executable generated.

or

B. Set environment variable $LD_LIBRARY_PATH before running the executables
This environment variable is similar to $LD_RUN_PATH, except that you need it when you run your executables. It can be considered as an extended search paths for OS when running executables.


If you don't want to re-compile/re-link your code, method B is convenient. However, the drawback is that your need to have your correct paths in $LD_LIBRARY_PATH every time your run your executables (of course you can choose to code that into your starting scripts like '.bashrc' to make sure it happens). Whereas method A is more like a once for all solution, as long as your shared library paths don't change.

 Afterthought:  due to the important role $LD_RUN_PATH played in the linking process, we need to make sure it has the correct value when linking. In my experience $LD_RUN_PATH has higher search priority over $LD_LIBRARY_PATH. So if there is a library in $LD_RUN_PATH specified during linking that is not the version want to use, you have no way to correct that using $LD_LIBRARY_PATH during running, and the only choice is to re-link the program after correct the value of $LD_RUN_LIBRARY.

No comments:

Post a Comment