**
今天,乙個同事編譯靜態庫,我也趁此機會在溫習一下,先google一下,然後在自己實驗。
首先,在網上抄個例子,內容如下
1:建靜態庫
/*hellos.h
*/#ifndef _hello_s_h
#define
_hello_s_h
void
prints(
char
*str);
#endif
/*hellos.c
*/#include
"hellos.h
"void
prints(
char
*str)
輸入命令:
gcc -c
-o hellos.o hellos.c
ar cqs libhellos.a hellos.o
於是得到了 libhellos.a這麼乙個靜態鏈結庫
2:主程式
/*main.c
*/#include
<
stdio.h
>
#include
"hellos.h
"main()
輸入命令:
gcc
-shared
-o libhellod.so hellod.c
於是得到了 libhellod.so 這麼乙個動態鏈結庫
4: 主程式
/*main.c
*/#include
<
stdio.h
>
#include
"hellod.h
"main()
上面的文章又說錯了,自己google一下,某人如下說:
在應用程式需要連線外部庫的情況下,linux預設對庫的連線是使用動態庫,在找不到動態庫的情況下再選擇靜態庫。使用方式為:
gcc test.cpp -l. -ltestlib
如果當前目錄有兩個庫libtestlib.so libtestlib.a 則肯定是連線libtestlib.so。如果要指定為連線靜態庫則使用:
gcc test.cpp -l. -static -ltestlib
使用靜態庫進行連線。
當對動態庫與靜態庫混合連線的時候,使用-static會導致所有的庫都使用靜態連線的方式。這時需要作用-wl的方式:
gcc test.cpp -l. -wl,-bstatic -ltestlib -wl,-bdynamic -ltestlib
【本段文字來自:
在轉他說的:
至此,解決該類問題。
附帶幾篇不錯的文章:
這小子的文章前面的還好,後面可給我害慘了】
GCC 編譯 靜態庫 動態庫
工作流程 命名格式 lib 庫的名字 so製作 第一步 得到 o檔案 引數 fpic表示生成與位置無關 gcc i.include fpic c c第二步 建立動態庫 shared 製作動態庫 o 重新命名生成的新檔案 gcc shared o lib so o使用 命名格式 lib 庫的名字 a製...
gcc編譯使用靜態庫 動態庫
假設已經有可用的liboutprint.a和liboutprint.so.1.0.0可用,則 gcc o hello static l.loutprint hello.cpp gcc o hello l.loutprint hello.cpp export ld library path pwd n...
gcc編譯靜態庫和動態庫
一 動態鏈結庫 1.建立hello.so動態庫 cpp view plain copy print?include voidhello 編譯 gcc fpic shared hello.c o libhello.so include void hello 編譯 gcc fpic shared hel...