[英]How to link and compile with .so file in Linux
我有.c和.so文件。 我嘗試使用以下編譯: gcc main.c -ldl
。 在那個.c文件中,我通過dlsym()
鏈接到.so文件。 如何使用.so文件與.c進行編譯。
可能您可以這樣做:
鏈接時:
g++ -o prog prog.o -ldllname
如果libdllname.so不在系統目錄中,則將其目錄添加到庫路徑中:
g++ -o prog prog.o -L/path/to/my/library/folder -ldllname
這是基於您的進一步評論。 首先要保護頭文件的聲明。
#ifndef HEADER_PROTECT
#define HEADER_PROTECT
---------- Here is the content of header
#endif
接下來,簽入您的代碼,是否要定義多個定義。 還是要重新定義標准功能? 您能否發布代碼以更好地指導您? 看起來您已經重新定義了Close_Comm(),可以檢查一下嗎? 錯誤說在main.c中也有定義。
以下是編譯共享對象並將其鏈接的一般方法。 編譯共享對象。
-g : for debug information
fPIC: for position independent code
$gcc -fPIC -g myfile
The following will create the shared object libmyfile.so
$gcc -shared -o libymyfile.so myfile.o
Now,In order to link it with your main.c.
I assume that the libmyfile.so is in your current path, thus -L./
$gcc main.c -o main.out -L./ -lmyfile
Now, you need to export the LD_LIBRARY_PATH on the bash; in order to execute the binary.
$LD_LIBRARAY_PATH=$LD_LIBRARAY_PATH:./
$./main.out
dlsym將在運行時從共享庫中加載符號。 如果要在運行時加載共享庫,則可以使用它。 以下是dlsym的示例之一, 在庫中修改標准函數,然后調用本機庫函數
dlsym()
用於在打開的庫文件中查找符號。 您首先需要使用dlopen()
來打開文件,然后才使用dlsym()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.