Ads 468x60px

##EasyReadMore##

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.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<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 libtest.so or lib….
gcc [options] -o

make file link with dynamic and static libraries « Waiting for the wind - https://goo.gl/jUFbA5

$ gcc main.c -L. -lmylib

linker會搜尋 libmylib.so 來進行連結。

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

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

ldd /sbin/fdisk <檢查程式所使用的 DLL>
linux-gate.so.1 => (0xffffe000)
libc.so.6 => /lib/libc.so.6 (0xb7669000)
/lib/ld-linux.so.2 (0xb77e1000)

程式扎記: [Linux 小學堂] Linux 的動態連結資料庫使用 - http://goo.gl/xNHwEC

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

0 意見:

張貼留言

 
Blogger Templates