简体   繁体   中英

My library does not load on other systems but works fine on the system it was compiled on

My project includes a library and example projects for how to use it. I place the library in the "bin" folder along with all executable examples. I can run the example projects on the machine where they were compiled but when I try to run them on another machine I get:

./example: error while loading shared libraries: libMyLib.so: cannot open shared object file: No such file or directory

This makes no sense since the library is in the same folder. What is causes it to ignore the library on other machines?

Just because the library is in the same directory as the executable doesn't mean it will look there for it. By default on linux, executables will only look in a limited set of directories, set by ldconfig and the LD_LIBRARY_PATH environment variable.

One trick that is very useful is to link your program with the extra linker option

-Wl,-rpath,'$ORIGIN'

which will cause the executable to also look in the directory the executable is in for shared objects.

You can usually set this by adding to your Makefile:

LDFLAGS := -Wl,-rpath,'$$ORIGIN'

Note the double- $ here -- make will interpret this as a make variable which expands to just $

The current directory is not necessarily a place where the dynamic linker will look for dynamic libraries. The directory where the executable is much less.

You might want to check ldconfig to see where it looks for them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM