Monday, October 30, 2006

Creating static and shared libraries in Linux

Am too bored to write a proper write-up as of now. So, here are the compiler commands to create static and shared libraries in linux (and mac too).

gcc -fPIC -g -c -Wall a.c
gcc -fPIC -g -c -Wall b.c
gcc -shared -Wl,-soname,libmystuff.so.1 -o libmystuff.so.1.0.1 a.o b.o -lc


Here, the bold and italicized stuff are those that you need to change to reflect to your library name and source files. In the above case, I am using two source files a.c, b.c and compiled it as a shared library named mystuff.so

To create a static library,
gcc -fPIC -g -c -Wall a.c
gcc -fPIC -g -c -Wall b.c
ar rc libmylib.a a.o b.o
ranlib libmylib.a

The last ranlib command indexes the symbols in the library archive to speed up compiler symbol lookup.