loading .so library C++

巧了我就是萌 提交于 2019-12-11 10:48:49

问题


I'm trying load library library.so, when i try if exist return true, but when I use dlopen return library doesn't exist.

std::ifstream ifile("library.so"); if (ifile) {
    cout << "Exist!" << std::endl; }

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

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

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

回答1:


dlopen is quite restricted in the paths it can search (to keep it short: the default paths plus LD_LIBRARY_PATH variable -- see the full documentation for a complete list). Your ifstream looks in the current directory (whatever it is), which is quite probably not included by default in the paths that dlopen considers.

Solutions include:

  • Setting LD_LIBRARY_PATH accordingly (which is usually the preferred method).
  • Using an absolute path instead of a relative one.
  • Putting your library in one of the default paths (eg. /lib or /usr/lib).


来源:https://stackoverflow.com/questions/16335671/loading-so-library-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!