程式扎記: [Linux 教學] make file link with dynamic and static libraries

標籤

2011年4月7日 星期四

[Linux 教學] make file link with dynamic and static libraries

轉載自 這裡 
* to build static library 

% gcc -c libtest.c -o libtest.o
ar rcs libtest.a libtest.o


* to build dynamic library 
-fPIC option tells gcc to create position independant code which is necessary for shared libraries 

% gcc -c -fPIC libtest.c -o libtest.o
gcc -shared -o libtest.so libtest.o


* to use static library 
% gcc -static main.c -L. -ltest -o testStatic 

* to use dynamic library 
% gcc main.c -L. -ltest -o testDynamic 

the source file of application who uses the library ltest, sould be located before -l options 

-L (no spacing after -L)
-o
-l (no spacing after -l)


@. in static link, library name is lib.a ex: libtest.a libcheckitout.a or libxxxx.a 
@. in dynamic link, library start with lib ex: libtest or libtest.so or lib…. 
gcc [options] -o 

補充說明 : 
@. 用 gcc 自製 Library

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!