bohanl
bohanl

Reputation: 1925

C++ dynamically linked library not found in executable

I have a .so in the path /home/test/lib/libTest.so and an executable called myexec that is built with RPATH pointing to /home/test/lib/. However, when I did ldd on myexec, it says libTest.so not found and I had to do export LD_LIBRARY_PATH in order for it to work.

I have confirmed:

  1. libTest.so does exist in /home/test/lib/
  2. objdump -x myexec | grep RUNPATH does indicate /home/test/lib/ is built into the executable.

Does anyone have idea why it couldn't find the library without setting LD_LIBRARY_PATH?

Upvotes: 2

Views: 4071

Answers (1)

learning_frog
learning_frog

Reputation: 315

From here:

In Linux, the environment variable LD_LIBRARY_PATH is a colon-separated set of directories where libraries should be searched for first, before the standard set of directories

As a tip, you should be careful not to replace entirely your LD_LIBRARY_PATH. It could mess up the path for the system shared libraries. Always use export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/your/path.

EDIT 1:

As for the rpath, make sure that libTest.so doesn't depend on any other library. As an alternative you can use -rpath-link option in gcc instead of -rpath, as seen here:

-rpath-link DIR

When using ELF or SunOS, one shared library may require another. This happens when an ld -shared link includes a shared library as one of the input files. When the linker encounters such a dependency when doing a non-shared, non-relocateable link, it will automatically try to locate the required shared library and include it in the link, if it is not included explicitly. In such a case, the -rpath-link option specifies the first set of directories to search.

EDIT 2:

If you want more help, please provide:

  1. Exact parameter for rpath used during compile
  2. Output for ldd /path/to/binary command
  3. Output for readelf -d /path/to/binary

Upvotes: 2

Related Questions