To further explain my situation. I know that when you’re using dynamic libraries, you need to export the library paths to LD_LIBRARY_PATH, so that the executable will find the libraries when they’re run. If I don’t do this, an error will come up, that the shared library could not be found.
Now if I add the following linker flag “-L/path/to/library/ -lthelibrary.so”, I can run my executable without exporting the library path to LD_LIBRARY_PATH.
Why is this?
First of all, you do not generally need to set
LD_LIBRARY_PATHto run a binary. This environment variable is used by the loader to find additional places to look for.sofiles to load when the binary is to be executed.You need to set
LD_LIBRARY_PATHif your binary references one or more.sofiles that are not available in the same location as when it was compiled. It is also needed if any of the directly referenced.sofiles depend on something that is not available in the same as when the.sofile was created.Use the
lddcommand to inspect the dependency information in your binary to get a clearer picture of what is saved by the linker when the binary is created.