We would like to create a link library containing the object code of the sum() and mul() functions written inside mymath.c source file, which can be called from different C programs.
- We produce the file object mymath.o:
$ gcc -c mymath.c
- Create static link library from mymath.o:
$ ar rcs libmylib.a mymath.o
- Static compile main.c with mylib.a as link library. Note that -L specifies the "current directory" library path, -l specifies the library, without the prefix and the suffix:
$ gcc -static main.c -L. –lmylib
- Now executable is ready to be executed:
$ ./a.out
Dynamic library has the prefix lib and the suffix .a
$ ar -t libmylib.a
mymath.o
$ ar -x libmylib.a
The full collection of the object files is extracted from static library, now we can build shared library.