Ads 468x60px


28 8月, 2016

GCC to create static and shared library

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.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<path to search libraries> (no spacing after -L)
-o <output file>
-l<lib_name> (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 or lib….
gcc [options] -o

make file link with dynamic and static libraries « Waiting for the wind -

$ gcc main.c -L. -lmylib

linker會搜尋 來進行連結。

如果目錄下同時有static與shared library的話,會以shared為主。
使用 -static 參數可以避免使用shared連結。

$ gcc main.c -static -L. -lmylib

ldd /sbin/fdisk <檢查程式所使用的 DLL> => (0xffffe000) => /lib/ (0xb7669000)
/lib/ (0xb77e1000)

程式扎記: [Linux 小學堂] Linux 的動態連結資料庫使用 -

Using GCC to create static and shared library .so @ CC :: 隨意窩 Xuite日誌 -

0 意見:


Blogger Templates