Creating .so and .a in UNIX

Tag: unix Author: p54188 Date: 2009-10-13

How to create a .so and .a file in UNIX. Do we have any standard utility for it?

Best Answer

*.a - archive library to create it compile your sources:

gcc -c -o foo.o foo.c
gcc -c -o boo.o boo.c

ar -rsc yourlib.a foo.o boo.o

so - position independent code shared library

gcc -fPIC -shared  -soname,libfoo.so.1 -o libfoo.so.1.0 foo.c boo.c

comments:

Any idea about .so file
second (-fPIC) creates *.so libraries, Your sources needs to be compiled this way, no other way (I don't know other ways).

Other Answer1

#create shared library
gcc -Os -fPIC -c test.c
gcc -shared  test.so test.o 


#create static library
gcc -Os -c test.c
ar rcs test.a test.o

Other Answer2

The .a is also called a static library, and the .so is also called a dynamically loaded library.

I like the Program Library HOWTO.

This HOWTO for programmers discusses how to create and use program libraries on Linux. This includes static libraries, shared libraries, and dynamically loaded libraries.

The Yo Linux tutorial is also useful.

This tutorial discusses the philosophy behind libraries and the creation and use of C/C++ library "shared components" and "plug-ins". The various technologies and methodologies used and insight to their appropriate application, is also discussed. In this tutorial, all libraries are created using the GNU Linux compiler.

Other Answer3

Take a look at this Makefile I wrote when I was new to C. It clearly shows how to generate and correctly link .a and .so files from a simple demo source.