Skip to content

Latest commit

 

History

History

static_library

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

Static Library

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.

  1. We produce the file object mymath.o:
$ gcc -c mymath.c
  1. Create static link library from mymath.o:
$ ar rcs libmylib.a mymath.o
  1. 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
  1. Now executable is ready to be executed:
$ ./a.out

Naming conventions

Dynamic library has the prefix lib and the suffix .a

Display contents of the archive

$ ar -t libmylib.a
mymath.o

Converting static library to dynamic library

$ ar -x libmylib.a

The full collection of the object files is extracted from static library, now we can build shared library.