簡體   English   中英

gnu鏈接器如何選擇要鏈接的動態庫

[英]How gnu linker choose which dynamic library to link

我當時使用GPU模擬器gpgpu-sim進行研究。 我自己的文件夾中有幾個.so文件: 我自己的文件夾

在Nvidia的cudart lib文件夾中有一些替代方法.so Nvidia的文件夾

當我鍵入命令時,有一些.o文件,需要與libcudart.so鏈接:

g++ -L "Path/to/MyFolder" -l cudart *.o

我希望生成的a.out可以鏈接到libcudart.so,但是它只是鏈接到一個奇怪的so文件:

    libcudart_gpgpu-sim_git-commit-6443f21d433f1b642003867e56fe1f54efae55e3_modified_0.so => not found

當我鍵入此代碼時:

g++ -L "Path/to/NvidiaFolder" -l cudart *.o

該程序可以在我的LD_LIBRARY_PATH文件夾中懷疑地找到libcudart.so.9,但是它表明版本不匹配!:

./a.out: /path/to/myFolder/libcudart.so.9.0: version `libcudart.so.9.0'not found (required by ./a.out) 

誰能告訴我ld工作原理以及如何解決這些問題?

我終於找到原因了。 如果使用此代碼鏈接對象以生成共享庫:

g++ -shared -Wl,-soname,libNAME_A.so -o libNAME_B.so

然后,如果有人嘗試使用以下方式鏈接NAME_B.so:

g++ <INPUT> -lNAME_B -o <OUTPUT>

輸出最終將查找libNAME_A.so。

請參閱g ++手冊頁:

  -Wl,option Pass option as an option to the linker. If option contains commas, it is split into multiple options at the commas. You can use this syntax to pass an argument to the option. For example, -Wl,-Map,output.map passes -Map output.map to the linker. When using the GNU linker, you can also get the same effect with -Wl,-Map=output.map. 

對於ld手冊頁:

 -soname=name When creating an ELF shared object, set the internal DT_SONAME field to the specified name. When an executable is linked with a shared object which has a DT_SONAME field, then when the executable is run the dynamic linker will attempt to load the shared object specified by the DT_SONAME field rather than the using the file name given to the linker. 

這里與CUDA無關,這只是一個鏈接和運行時環境設置問題。

ld鏈接程序按照-L選項參數指定的順序搜索對象和庫歸檔文件,並且僅在進入默認系統目錄之后才進行搜索。 鏈接器將鏈接與該搜索最匹配的目標代碼。

在運行時,如果您鏈​​接到動態庫(.so文件),則需要正確定義LD_LIBRARY_PATH環境變量,並使用路徑列表查找動態庫,並用冒號(:)分隔。

因此,如果您使用本地路徑中的庫鏈接到對象(假設您正在尋找libcudart.so):

g++  -o myprogram *.o -L "/Path/to/myFolder" -lcudart

您需要在運行程序之前按以下方式設置LD_LIBRARY_PATH:

export LD_LIBRARY_PATH="/Path/to/myFolder:$LD_LIBRARY_PATH"
./myprogram

希望對您有所幫助,並闡明您的理解。 坦白說,我不了解您的libcudart_gpgpu-sim_git-commit匹配的來源

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM