Create a Library in C

Create a Static Library

create object file

gcc -c -o test.o test.c

create a static library from object file

ar rcs libtest.a test.o

create the executable file

gcc -o program program.o libtest.a

or

gcc -o program program.o -L. -ltest

-L.: add local directory to the lib path.
-ltest: link the libtest.a static library.

Difference between static library and dynamic library

Static library link all required functions or libraries into the executable file, while dynamic libraries are linked during running time.

In Linux, static library is named *.a, dynamic library is named *.so.
In Windows, static library is named *.lib, dynamic library is named *.dll.

lib in Linux

lib in C

Add another to our library

ar rcs libtest.a test.o test2.o