BSalunke
BSalunke

Reputation: 11737

How to check shared library is loaded successfully or not loaded using dlopen?

Im loading a shared library using dlopen() function in C++ program.

Then how to check it is successfully loaded or not? or Can we check that loading of library using mangled name of any function present in that library?

Upvotes: 1

Views: 2737

Answers (6)

Nitin Trivedi
Nitin Trivedi

Reputation: 31

You can also add a static block in one of the files of your shared library. When it is loaded successfully, it will print a message. Example :

struct LoadMessage {
        init(void){
        std::cout << "I am loaded\n";
        }
};

LoadMessage message;

Upvotes: 0

nos
nos

Reputation: 229184

If it's not successfully loaded, dlopen() returns NULL.

The man page for dlopen() says:

RETURN VALUE

If file cannot be found, cannot be opened for reading, is not of an appropriate object format for processing by dlopen(), or if an error occurs during the process of loading file or relocating its symbolic references, dlopen() shall return NULL. More detailed diagnostic information shall be available through dlerror() .

Upvotes: 1

Saqlain
Saqlain

Reputation: 17928

As every one mentioned if dlopen() fails you get a null, but if you trying to solve why you getting mysterious null even though library is present at the path you specified, may sure all libraries are present on the system on which that "particular library" depends, otherwise call would fail with a NULL ;)

Go for using extern "C" for the function in your .cpp file so compiler don't mangle it, and you can call it once library is loaded.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409266

From the manual page:

If dlopen() fails for any reason, it returns NULL.


The dlsym function can not handle C++ identifiers, unless they have been declared extern "C", or by you using the mangled name.

Upvotes: 1

Arne Mertz
Arne Mertz

Reputation: 24626

If dlopen encounters an error, it returns NULL and dlerror will return a corresponding error message.

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249374

According to the documentation (http://linux.die.net/man/3/dlopen), dlopen returns NULL on any failure. So you can just check for that and be done. But if for some reason you don't trust that the library is a "good" one, you could define your own convention for your system. For example, you could require that any library loadable by your system define a symbol with a certain name which has some specific properties, up to and including something like an "API key" that you could assign to users who want to write compatible libraries.

Upvotes: 0

Related Questions