在GCC中已經指定連接庫,然而編譯時卻提示動態庫函數未定義!shell
測試出現的錯誤提示以下:函數
[GMPY@13:48 tmp]$gcc -o test -L. -lmylib test.c /tmp/ccysQZI3.o:在函數‘main’中: test.c:(.text+0x1a):對‘func_lib’未定義的引用 collect2: error: ld returned 1 exit status
而在測試用的動態庫libmylib.so
中是有定義函數func_lib
的測試
[GMPY@13:55 tmp]$cat mylib.c #include <stdio.h> int func_lib(void) { printf("In share library\n"); return 0; } [GMPY@13:56 tmp]$gcc -fPIC -shared mylib.c -o libmylib.so
在用gcc編譯時,咱們能夠用-L
指定連接庫位置,用-l
指定。優化
man gcc
查詢時,我發現這麼一段描述:this
-llibrary -l library ... ## 這裏爲了方便閱讀,對原文進行了換行排版優化 It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded. ...
嗯,這段話什麼意思呢? 若是-l
連接庫在源碼以前,就會連接不到庫!!code
就像下面兩個命令的差異:ci
異常:gcc -o test -L. -lmylib test.c 正常:gcc -o test -L. test.c -lmylib
居然對執行時參數的位置都有要求,也是醉了源碼