How to make a static library (.a) on Linux

How to make a static library (.a) on Linux?

How to generate a library from C programs in files lib1.c and lib2.c:

$ gcc -c lib1.c lib2.c

Create a library “libmy.a” using ar:

$ ar -cvq libmy.a lib1.o lib2.o

You can also list the files in a library with ar:

$ ar -t libmy.a

You can link the library in your program (e.g. p.c):

$ gcc -o p p.c libmy.a

or

$ gcc -o p p.c -L/path/to/libmy.a/directory -lmy
Leave a Reply

Your email address will not be published. Required fields are marked *