This post has already been read 2009 times!

Static Shared Library in C - Linux

1. Create a C file with a function - example :

#include "stdio.h"
int addnumbers(int a , int b)
int total;
total = a + b;
return total;

2. The next step is to complile this file :

gcc -c moo.c

3. Use the ar (man ar - create, modify and execute from achives)

ar -cvq libmoo.a moo.o

4. The libmoo.a static library will be created.

5. Display the symbols for the object files for libmoo.a with

nm libmoo.a

6. Use the ranlib libmoo.a - for run the library

7. Now create the main program - foo.c that will use the static library

#include "stdio.h"
#include "iostream.h"
#include "string.h"
using namespace std;
//declare the function inside the program
int addnumbers(int a , int b);
int main()
int total = addnumbers(4,10);
printf("the sum is : %d", total);
return 0;

8. Compile the main program using the static library libmoo.a

gcc -o foo foo.c libmoo.a

9. Run the program to check if all are OK :

./foo ---> // that will display the sum is : 14 if all are OK

Static Shared Library in C - Linux

Comments are closed.

Post Navigation