ldd says library isn't found by compile completes successfully

I just stumbled upon this, had the same problem but a different solution.

Using LD_LIBRARY_PATH will, in fact work. And it is fine if it is for your own testing in your build environment, but you should try to avoid it besides for a case like this. Here is an article by someone who knows much more than me about it, why LD_LIBRARY_PATH is bad:

http://xahlee.info/UnixResource_dir/_/ldpath.html

What happened is, as can be seen also from the fact that setting LD_LIBRARY_PATH worked, is that at runtime, your program could not find the shared library libtier0_srv.so. Instead of globally setting a variable for all programs to look at /home/dev/sdks/hl2sdk-ob-valve/lib/linux/ first, you should add the directory to the runtime library search path. You do this by passing the option

-rpath /home/dev/sdks/hl2sdk-ob-valve/lib/linux/

to ld, the linker. You can do this with your gcc command you posted, by adding the option

-Wl,-rpath,/home/dev/sdks/hl2sdk-ob-valve/lib/linux/,

which tells gcc to pass the option above to ld.


The library files are shared objects, which means that they will not be resolved until run time. In order for ldd to find them (assuming Linux or other Unix variant) you will need to add the path the libraries to your LD_LIBRARY_PATH (there is another path env that can be used but I can't think of it right now) and then ldd should be able to find the library.


As @diverscuba23 mentioned, you need to add the path your library is located at to your LD_LIBRARY_PATH. An easy, and non permanent way of doing this is specifying it when you run the program like so:

LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH ./yourProgram

In this case the library would need to be within the same directory you are running the program.

More generally:

LD_LIBRARY_PATH=<PATH_TO_YOUR_LIBRARY>:$LD_LIBRARY_PATH ./yourProgram

Tags:

C++

Makefile

Gcc