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);
}