2013-05-02 62 views
1

我想加載庫library.so,當我嘗試如果存在返回true,但是當我使用dlopen返回庫不存在。加載.so庫C++

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; } 
+0

可能是ifstream對象鎖定了文件對象,請在使用dlopen之前嘗試執行'ifile.close()'。 – Robert 2013-05-02 10:33:41

回答

2

dlopen它可以搜索的路徑是相當的限制(以保持其短:默認路徑加LD_LIBRARY_PATH變量 - 看到完整的documentation完整的列表)。您的ifstream在當前目錄中查找(不管它是什麼),這在dlopen考慮的路徑中很可能不包括默認值。

解決方案包括:

  • 相應設置LD_LIBRARY_PATH(這通常是優選的方法)。
  • 使用絕對路徑而不是相對路徑。
  • 將您的磁帶庫置於其中一個默認路徑中(例如,/lib/usr/lib)。