我想建立一個C程序,需要在PostgreSQL libecpg-dev軟件包下列庫的靜態可執行文件:libecpg,那麼libpq和libpgtypes。靜態庫libecpg.a,libpq.a和libpgtypes.a都與動態版本/ usr/lib位於同一位置。無論如何,我嘗試添加-L選項作爲他們的路徑,但我得到了相同的結果。鏈接到靜態庫來構建靜態可執行
我的可執行文件必須是靜態的,這樣二進制可以手提,使用戶不必安裝庫到他們的系統中使用它。當我使用下面的命令與動態庫該方案建立罰款:(使用-i目錄必須包括對於一些PostgreSQL函數我使用)
gcc main.o -lecpg -lpq -lpgtypes -I/usr/include/postgresql -o program_name
當我嘗試鏈接到靜態圖書館,我使用下面的命令:
gcc -static main.o -lecpg -lpq -lpgtypes -I/usr/include/postgresql -o program_name
編譯器,但是,給我的「未定義的引用」錯誤,輸出一個長長的清單。有這麼多,但這裏是一個小樣本:
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libecpg.a(descriptor.o):
In function `get_descriptors':
(.text+0xc3): undefined reference to `pthread_once'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libecpg.a(descriptor.o): In function `ECPGdeallocate_desc':
(.text+0x2c6): undefined reference to `pthread_setspecific'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libecpg.a(descriptor.o): In function `ECPGallocate_desc':
(.text+0x376): undefined reference to `pthread_setspecific'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libecpg.a(descriptor.o): In function `get_descriptors':
(.text+0xd2): undefined reference to `pthread_getspecific'
當我更改順序如下:
gcc -I/usr/include/postgresql -static -lecpg -lpq -lpgtypes -o program_name main.o
編譯器輸出,而不是看起來像下面的(再次,只是一小部分):
main.c:(.text+0x5c6): undefined reference to `ECPGconnect'
main.c:(.text+0x83e): undefined reference to `ECPGdo'
main.c:(.text+0x843): undefined reference to `ECPGget_sqlca'
main.c:(.text+0x85c): undefined reference to `ECPGdisconnect'
main.c:(.text+0x87c): undefined reference to `ECPGget_sqlca'
我已經嘗試使用-I選項四周(以及所有其他選項),但似乎沒有任何工作。