I am curious about using dlopen in Linux to call shared libraries.
Suppose I want to use a shared library in C whose name is fileName.so. I am working in a 64bit Ubuntu Linux and I include dlfcn.h and use dlopen function to access the shared library.
When I use dlopen(fileName.so, RTLD_LAZY), a NULL handle is returned and shared library is not opened. However, when I use dlopen("./fileName.so", RTLD_LAZY) the dlopen does its job and opens the shared library. It seems that the main point is in using ./ before file name.
It is appreciated if help me figure out why I should use ./ in my code. Thanks
POSIX says that
dlopen()has to know where to look for the file and leaves the behaviour when the file name does not include a/implementation defined. On Linux, if you don’t supply a pathname (a name with a/in it somewhere), thendlopen()only looks in ‘standard places’, specified by environment variables such as LD_LIBRARY_PATH or via/etc/ld.so.conf(or/etc/ld.so.cache; see alsoldconfig(8)) or in standard places such as/liband/usr/lib.When you specify the relative name
./fileName.so, it knows to look in the current directory, which is not normally a place it looks.Note that you can run into some interesting issues on systems that support both 32-bit and 64-bit executables, with various conventions being used for the locations of the different classes of library. Other variants of Unix use vaguely related systems — mostly using
dlopen()et al these days (historically, it was not always thus), and using a wide variety of environment variables (DYLD_LIBRARY_PATH, LIBPATH, SHLIB_PATH, LD_RUN_PATH, LD_LIBRARY_PATH_32, LD_LIBRARY_PATH_64, …).