How to make a C library


Suposse you want to create a library called ‘libhello’ that provides an ‘hello’ function to include in your program ‘main.c’.

Start by writing ‘hello.c’, e.g.:

void hello ();

int
main (int argc, char *argv[])
{
hello ();
}

And the ‘main.c’,e.g.:

#include

void
hello ()
{
printf ("Hello!n");
}

Compile ‘hello.c’ into an object with the ‘-c’ option to ‘gcc’:

$ gcc -c hello.c

Create the library using ar(1) e ranlib(1):

$ ar cru libhello.a hello.o
$ ranlib libhello.a

And finally compile your program using the new library you’ve created:

$ gcc -o hello main.c libhello.a
$ ./hello
Hello

References:
* [[http://sourceware.org/autobook/autobook/autobook_toc.html|The Goat Book]] – Freely available book describing how to use GNU Autoconf, Automake, and Libtool.
* The ar(1) e ranlib(1) manpages.