Tuesday, May 27, 2008

Static, Shared Dynamic and Loadable Linux Libraries

There are two kinds of library in Linux:
static library(.a) and dynamic library(.so)
The difference between them is
1, static library will link with and become a part of application. But the dynamic library just be used when compile and tied to application.
2, Many setup can use a same dynamic library at a same time. But linux just load them once.
3, You can load,unload and link dynamically library with application by system function

How to build the static library:
ar -cvq libliutest.a cliutest1.o cliutest2.o
List it:
ar -t libcliutest.a
Link it:
  • cc -o myprogram progLiu.c libcliutest.a
  • cc -o myprogram progLiu.c -L/path/to/library-directory -lcliutest

How to build the dynamically library:

    gcc -Wall -fPIC -c *.c
gcc -shared -Wl,-soname,libcliutest.so.1 -o libcliutest.so.1.0 *.o
mv libliuctest.so.1.0 /opt/lib
ln -sf /opt/lib/libcliutest.so.1.0 /opt/lib/libcliutest.so
ln -sf /opt/lib/libcliutest.so.1.0 /opt/lib/libcliutest.so.1

see more website:"http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html"

No comments: