![](/img/trans.png)
[英]How to know if one shared library depends on another shared library or not?
[英]Creating shared library that depends on another shared library using ld
我想創建一個共享庫libdependent
,該庫使用使用頭文件的libparent
從某些導出的函數。
libparent
的路徑在構建階段未知,因此我不能使用rpath
,而是調用dlopen("path/libparent.so", RLTD_NOW | RTLD_GLOBAL)
和dlopen("path/libdependent.so", RLTD_NOW | RTLD_GLOBAL)
。
但是,如果我在鏈接和使用-lparent
期間將libparent.so
放入庫搜索路徑中, libparent
在libdependent
文件中根本沒有對libparent
引用。
當我嘗試dlopen
libdependent
,即使設置了RTLD_GLOBAL
,我libdependent
收到“無法定位符號”錯誤。
我應該如何使用libparent
而不調用dlsym
?
首先,當您要創建庫時,無需導入它,因此
dlopen(“ path / libdependent.so”,RLTD_NOW | RTLD_GLOBAL)
不需要。
其次,如果您不確定要使用的庫名(libparent),則必須使用動態鏈接和dlopen 。 在動態鏈接中,您不必向鏈接程序通知您的libparent庫,但是您必須使用動態鏈接程序庫,因此鏈接程序命令將如下所示:
g ++ -o輸出-dl input.cpp
dl表示您將使用dlopen 。
在使用程序時,請確保您的libparent.so在運行目錄中可見(或使用絕對路徑)。 還要檢查dlopen的返回值以了解成功打開庫的信息。
void *handle = NULL;
handle = dlopen("libparent.so", RTLD_LAZY);
if(!handle){
printf("Error!\r\n");
}
希望能幫助到你。
通過在鏈接器選項中添加-shared
並在-l
指定庫來解決。 -(
也可能有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.