juzzlin
juzzlin

Reputation: 47945

Linux: Why loader finds my shared library?

I have compiled a shared library with CMake as a sub project and the main app then links to the library. The library and application are in the same output directory somewhere under my home dir.

Because I'm on Linux I don't now understand why the loader sees my library.

When I check libs with ldd everything is ok. However, I was under the impression that I have to set LD_LIBRARY_PATH so that my application can load the shared lib from the same directory. But I haven't set it and it still works. Why?

Upvotes: 2

Views: 204

Answers (1)

John Zwinck
John Zwinck

Reputation: 249542

Perhaps your build process is setting RPATH inside your executable to look for the library in the same directory. To test this, try moving the executable to a different directory and see if you can run it (or ldd it) then.

You can also check for RPATH in an executable in either of these ways:

readelf -d the-exe | grep RPATH
objdump -x the-exe | grep RPATH

For more on this, see here: https://unix.stackexchange.com/questions/22926/where-do-executables-look-for-shared-objects-at-runtime

Upvotes: 3

Related Questions