Create C Program with Static Library in Linux

Consider the following C program:

#include <stdio.h>
#include <math.h>

double tvm (double rate, int terms, double principal);

int main ( )
{
 double myrate;
 int myterm;
 double myprincipal;
 double myreturn;

 printf ("Please enter the following:\n");
 printf ("Rate of return:(divide by 100, 4% is 0.04) ");
 scanf ("%lf", &myrate);
 printf ("\n");

 printf ("Number of terms:(No decimals): ");
 scanf ("%d", &myterm);
 printf ("\n");

 printf ("The principal amount: ");
 scanf ("%lf", &myprincipal);
 printf ("\n");

 myreturn = tvm (myrate, myterm, myprincipal);
 printf ("Your return after %d term is $%.2lf.\n", myterm, myreturn);

 return 0;
}

double tvm (double rate, int terms, double principal)
{
 double dn;
 double pp;
 dn = terms;

 pp = pow (1+rate, dn);
 return principal * pp;
} 

You can place the tvm (Time Value of Money) function into a static library so that you can reuse the function.

To create a static library, you need to place the function in mymath.c and create a separate header file mymath.h.

Note: You can have many C program file with just one header file such as lib.h, lib1.c, lib2.c ... and so on.

The header file mymath.h is as follows:

#include <math.h>

double tvm (double rate, int terms, double principal);

The program file for tvm function mymath1.c is as follows:

#include "mymath.h"

double tvm (double rate, int terms, double principal)
{
 double dn;
 double pp;
 dn = terms;

 pp = pow (1+rate, dn);
 return principal * pp;
} 

To compile the tvm fumction program use the command:
gcc -Wall -c mymath1.c 

-c stands for compile only
The compiler create an object file mymath1.o

Create Static Library

You can create static library using the command ar. This command also allows you to add additions library functions.

Library Naming Convention
The naming convention of a library package must be prefix with lib follow by the name of the library and the file must end with .a. Therefore a typical library should be named as libmymath.a. 

Common usage of ar command is as follows:
ar rcs libmymath.a mymath1.o

Options:
r - insert the object files to archive
c - create archive if not exist
s - create index

You can append additional object file to the library file as follows: 
ar rcs libmymath.a mymath2.o

Create the Application Program

Create the following application program named as app1.c:

#include <stdio.h>
#include "mymath.h"

int main ( )
{
 double myrate;
 int myterm;
 double myprincipal;
 double myreturn;

 printf ("Please enter the following:\n");
 printf ("Rate of return:(divide by 100, 4 percent is 0.04): ");
 scanf ("%lf", &myrate);
 printf ("\n");

 printf ("Number of terms:(No decimals): ");
 scanf ("%d", &myterm);
 printf ("\n");

 printf ("The principal amount: ");
 scanf ("%lf", &myprincipal);
 printf ("\n");

 myreturn = tvm (myrate, myterm, myprincipal);
 printf ("Your return after %d term is $%.2lf.\n", myterm, myreturn);

 return 0;
}

Note: Please note that you can use #include "mymath.h" or  #include "~/Documents/cprog/include/mymath.h", depending on where your header file is.

Compile The Application

If static library file and header file are in the same folder then use any of the following command:
gcc -Wall app1.c -o app1 -lm -lmymath -L ./
or
gcc -Wall app1.c -o app1 -lm libmymath.a

Options:
-l option follow by library name indicate which static library to link, note that this option automatically add prefix lib and append .a, If you have more than one library file you must indicate it separately.
-L locates the path of library

If your file libmymath.a is located in /usr/lib or /usr/local/lib (man ld to see search path) then you no need -L, you can just use
gcc -Wall app1.c -o app1 -lm -lmymath

If your library file and header file are located in separate folder then
gcc -Wall app1.c -o app1 -lm -lmymath -I ~/Documents/cprog/include/ -L ~/Documents/cprog/slib/

Additional Tools 

Besides gcc and ar, you can also use ranlib. Ranlib is equivalent to the command ar -s. There is also another tool known as ld. This is the basic linker which usually runs behind the scene when you run gcc, use man ld to see default search path for static library.

Library Display Tools
Another useful tool is nm. This tool display object files or library file. The command ar can also be use to display object file.

To check what is inside libmymath.a, use the command:
ar t libmymath.a 

Note: The results will be a list of object files <filename>.o

Alternatively, you can use the following command: 
nm -s libmymath.a

Note: This is the better tools because besides object files it also list the function names. 

Special Notes:

Do we need to create a library file?
When you use the command ar to create a library file, it is basically an archive of many object files. A library file is useful for packaging and distribution purpose.

You can always compile your program without library file and use object files. The above program can be compile as follows:
gcc -Wall app1.c mymath1.o -o app1 -lm

As you create more and more object files, then it is easier to group all the similar object files into a single library file. It is easier to use library file to distribute to other software developers instead of multiple object files.



*****

Comments

Popular posts from this blog

Revive Old Mac Mini (2009) with Linux

Configure Unattended Upgrades on Raspberry Pi

Install and Configure RealVNC in Linux Ubuntu 18.04 LTS