First, create separate source files for two functions (name FRED.C and BILL.C respectively). The following first source file:
#include "stdio.h"
void Fred (int arg)
{
printf ("Fred:we passed%d\n", ARG);
}
Here is the second source file:
#include "stdio.h"
void Bill (Char *arg)
{
printf ("Bill:we passed%s\n", ARG);
}
Second, compile these functions separately to produce the target files to be included in the library file.
$ gcc-c bill.c FRED.C
Iii. Write a program that calls the Bill function now. First create a header file for your library file. This header file will declare the functions in your library file, and he should be included with all applications that want to use your library files.
/*this is Lib.h It declares the functions Fred and bill for users * *
void Bill (char *);
void Fred (int);
The calling program (PROGRAM.C) is very simple, it contains the header file of the library and calls a function in the library.
#include "Lib.h"
int main ()
{
Bill ("Hello World");
Exit (0);
}
Five, now you can compile and test this program. You temporarily display the specified destination file for the compiler, and then ask the compiler to compile your file and link it to the previously compiled target module BILL.O.
$ gcc-c PROGRAM.C
$ gcc-o Program PROGRAM.O BILL.O
$./program
Bill:we passed Hello Word
$
Six, now, you will create and use a library file. Use the AR program to create an archive and add your target file.
$ ar crv libfoo.a bill.o fred.o
A-bill.o
a-fred.o
Seven, the library file is created, two target files are added, in order to successfully use the function library, you also need to generate a table of contents for the function library. You can use the Ranlib command to do this work.
$ ranlib LIBFOO.A
This library can be used, and you can add the library file to the list of files used by the compiler to create your program.
$ gcc-o Program PROGRAM.O LIBFOO.A
./proram
Bill:we passed Hello World
$
You can also use the-l option to access the function library, but it does not have a standard location, so you must use the-l option to tell the compiler where to find him,
$ gcc-o Program program.o-l.-lfoo