Question

I have myLib.so file and USB.h header file. My header file looks like this and myLib.so contains implementation of this header file. How can I use myLib.so to call getUsbList function in my main.cpp.

#ifndef USB_H
#define USB_H
#include <string.h>
#include <string>
#include <vector>

vector<string> getUsbList();

#endif // USB_H

I try this but it gives error: Cannot load symbol 'getUsbList': myLib.so: undefined symbol: getUsbList

#include <iostream>
#include <dlfcn.h>
#include "USB.h"

int main() {
    using std::cout;
    using std::cerr;

    cout << "C++ dlopen demo\n\n";

    // open the library
    cout << "Opening myLib.so...\n";
    void* handle = dlopen("myLib.so", RTLD_LAZY);

    if (!handle) {
        cerr << "Cannot open library: " << dlerror() << '\n';
        return 1;
    }

    // load the symbol
    cout << "Loading symbol myLib...\n";
    typedef void (*USB_t)();

    // reset errors
    dlerror();
    USB_t getUsbList = (USB_t) dlsym(handle, "getUsbList");
    const char *dlsym_error = dlerror();
    if (dlsym_error) {
        cerr << "Cannot load symbol 'getUsbList': " << dlsym_error <<
            '\n';
        dlclose(handle);
        return 1;
    }

    // use it to do the calculation
    cout << "Calling getUsbList...\n";
    getUsbList();

    // close the library
    cout << "Closing library...\n";
    dlclose(handle);
}
Was it helpful?

Solution

Looks like you're using C++

vector<string> getUsbList();

C++ applies so called "name mangling" to give each function symbol a unique name based on the types of its input and output parameters. This is done to support function overloading (multiple functions of the same name but with different parameter types).

You either have to load based on the mangled name or you have to disable name mangling (and hence the ability to overload the function).

Since there is no standard on how names are mangled and each compiler may implement it differently this is not a very robust method.

Disabling name mangling is done by surrounding the function declarations in the header with

extern "C" {

// ...

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top