简体   繁体   中英

Calling a shared library using ./

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), then dlopen() 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 also ldconfig(8) ) or in standard places such as /lib and /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, ...).

./ is a relative path to the .so file. It means that the file is in the current directory.

In *nix, by default, when given a file name without an absolute or relative path, dlopen will search for the library in a set list of default locations .

The "main point" is in using double-quotes in your second example:

    dlopen("./fileName.so", RTLD_LAZY)

If you want to include your own library/filename enclose it in double-quotes. You don't even need the ./ for that, provided that the file is in the current directory, as ./ suggests.

As per the dlopen manpage's example:

    handle = dlopen("libm.so", RTLD_LAZY);
       if (!handle) {
           fprintf(stderr, "%s\n", dlerror());
           exit(EXIT_FAILURE);
       }

the filename is enclosed in quotes.

Though, as specified by a previous answer, dlopen will look in the "standard" places for "includes". Another way of including a library that's inside your working directory (though, obviously, not a shared system library) is to use the preprocessor directive with the filename enclosed within double quotes:

#include <stdio.h>
#include <stdlib.h>
#include "myCustomLibrary.h"

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