我想學習使用GCC的Linux上的共享庫概念。所以我創建了一個簡單的庫。創建一個簡單的共享庫
LIBRARY.C
int foo(void) {
return 10;
}
這是用匯編,
cc -fPIC -g -c library.c
cc -shared -fPIC -Wl,-soname,libmytest.so.1 -o libmytest.so.1.0.1 library.o -lc
它創建的文件libmytest.so.1.0.1
在當前目錄。現在我正在編寫一個客戶端來在同一個目錄中使用這個庫。
client.c
#include <stdio.h>
extern int foo(void);
int main()
{
int a = foo();
printf("a is %d", a);
return 0;
}
使用編譯
cc client.c -o client -lmytest
,但是這將退出與消息
/usr/bin/ld: cannot find -lmytest
collect2: ld returned 1 exit status
誰能幫我找出我在做什麼這裏錯了嗎?
謝謝,但沒有運氣。仍然是同樣的錯誤。我什至試圖指定完整的路徑,但沒有奏效。 – 2010-11-14 15:36:33
我必須運行'ldconfig'嗎? – 2010-11-14 15:39:18
我通過直接提供庫名稱而不是通過-l來實現它。 – 2010-11-14 15:45:44