linux下編寫和呼叫乙個簡單的動態庫大概分為以下幾個步驟:
int add(int a,int b)
#ifndef _head_
#define _head_
int add(int a,int b);
#endif
gcc -fpic -c add
.c
gcc -shared –o libhead.so
add.o
#include
#include "head.h"
int main(void)
gcc -o main main.c -l./
-lhead
-l指定動態鏈結庫的路勁,-ldxx鏈結庫函式xx。-lxx是動態庫的呼叫規則。linux系統下的動態庫命名方式是lib*.so,而在鏈結時表示位-l*,*是自己命名的庫名。
error while loading shared libraries: libhead.so: cannot open
shared object file: no such file
or directory
這是因為程式執行時沒有找到動態鏈結庫的原因,注意linux不會自動在當前目錄查詢動態庫檔案。解決方案一般有三種,借鑑smartvessel原文:
(1.) 用ln將需要的so檔案鏈結到/usr/lib或者/lib這兩個預設的目錄下邊
ln -s /home/使用者名稱/code/lib/*.so /usr/lib
sudo ldconfig
注意填寫你的動態庫所在目錄的路徑,若不知道可以用pwd檢視全路徑。如果路徑不對,可能會報錯:
cannot open shared
object file: error
40
(2.) 修改ld_library_path
export ld_library_path=/home/使用者名稱/code/lib/:$ld_library_path
sudo ldconfig
(3.) 修改/etc/ld.so.conf,然後重新整理
vim /etc/ld
.so.conf
add /home/使用者名稱/code/lib/
sudo ldconfig
必須記得更新sudo ldconfig,否則還是會報錯。
./main
3+5 sum =8
Linux 下Python呼叫C 編寫的動態庫
在工程中用到使用python呼叫c 編寫的動態庫,結果報如下錯誤 oserror extract str.so undefined symbol znst8ios base4initd1ev python呼叫函式 1 coding utf 8 2from ctypes import 34 libpc...
Linux環境下動態庫的呼叫
1 開啟動態鏈結庫 dlopen,函式原型為 void dlopen const char filename,int flag dlopen用於開啟指定名字 filename 的動態鏈結庫,並返回操作控制代碼。dlclose用於關閉指定控制代碼的動態鏈結庫,只有當此動態鏈結庫的使用計數為0時,才會真...
linux下動態呼叫靜態庫的方法
linux下動態庫呼叫靜態庫的方法 有這樣一種情形,在建立乙個動態庫的同時,可能會呼叫乙個靜態庫,這個靜態庫可能是你自己寫的,也可能是第三方的。比如有下面五個檔案,生成乙個靜態庫,乙個動態庫,乙個執行檔案 static.h void static print include include stat...