Thursday, August 15, 2013

Compiling C Program in Linux Part 2

In the second part, we will write and compile the same program using separate headers file and program. This exercise is useful when you are teaming with your colleague or classmate working on a single project. We will also attempt to use makefile for our compilation.

Writing and Compiling C Program using Multiple Programs

We can convert the following program into multiple programs:

#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: ");
 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;
} 


To split this program, we will keep the functions in myfinancial.h and myfinancial1.c. Then we keep the main program in finapp1.c.

The purpose of splitting the program is to allow tvm function to be reuse in another program.

In the file myfinancial.h, we will include all the financial functions that we have created.

#include <math.h>

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

The file myfinancial1.c will contains the tvm function.

#include "myfinancial.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 function program
gcc -Wall -c myfinancial1.c -o myfinancial1.o -lm 

This command will just create the object file as myfinancial1.o.

Once the program is compiled, we do not need to compile it again unless there is changes. You can copy the object file myfinancial1.o to your fellow colleague or classmate for testing.

Now, we can write the main application program in finapp1.c as follows:

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

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

 printf ("Please enter the following:\n");
 printf ("Rate of return: ");
 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;
}

To compile finapp1.c:
gcc -Wall finapp1.c myfinancial1.o -lm -o finapp1 


With the object file available, we can create another program finapp2.c using the same tvm function:

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

int main ()
{
 double rate = 0.025;
 int term = 10;
 double principal = 1000;
 double result = 0;

 result = tvm (rate, term, principal);

 printf ("The return of $1000 investment with a rate of 2.5 percent in 10 years will give you $%.2lf.\n", result);
 return 0;
}

To compile finapp2.c:
gcc -Wall finapp2.c myfinancial1.o -lm -o finapp2 


Group Programming
In a large programming project involving a team, you need to design the basic structure of the programs. Then you decide on all the necessary functions required in the program. You can also group all the functions into several modules. Each programmer will be responsible for each modules. Each modules contains a header file and a program file. It is important that the header file is completed during the design phase. Using the above method, you can compile the program when all functions and modules are completed.


Compiling C Program using Makefile

Using the example in the previous section, instead of compiling line by line, we can create a make file that will follow certain compiling instructions.

The format for the make file is:
target: dependencies
     command

Note:
There must be a tab before command.
The makefile is usually named as makefile or Makefile

To create the makefile of previous program finapp1.c, the makefile is as follows:

#Creating Program
finapp1: finapp1.o myfinancial1.o
 gcc finapp1.o myfinancial1.o -lm -o finapp1

#Creating Objects
finapp1.o: finapp1.c myfinancial.h
 gcc -c finapp1.c -o finapp1.o

myfinancial1.o: myfinancial1.c myfinancial.h
 gcc -c myfinancial1.c -lm -o myfinancial1.o

clean:
 rm -f finapp1 finapp1.o myfinancial1.o

This is the more explicit way to instruct the make program how to compile the programs line by line. Please note that the final program must come first before compilation of object. It is a drill down approach.

After the make file is written, you can package all the program and makefile together.

To compile the program just type
make

To remove the compiled program and object type
make clean

However, we need not be so explicit because make program know how to compile programs. We can set the compile flags and the linking flags as follows:

CFLAGS=-Wall
LDFLAGS=-lm

#Creating Program
finapp1: finapp1.o myfinancial1.o myfinancial.h

clean:
 rm -f finapp1 finapp1.o myfinancial1.o

Note:
CFLAGS refers to complie flags
LDFLAGS refers to linking flags

By indicating our target program and the dependencies, the make program will compile the program and link them appropriately using the flags indicated. You need to supply finapp1.c, myfinancial1.c, myfinancial.h and makefile.

Since the functions object file need not be compiled every time, we can use the object file with the the main C program. The makefile is the same except that under clean section you don't remove the object file myfinancial1.o. You will be supplying myfinancial1.o instead of myfinancial1.c.

The makefile will be as follows:

CFLAGS=-Wall
LDFLAGS=-lm

#Creating Program
finapp1: finapp1.o myfinancial1.o myfinancial.h

clean:
 rm -f finapp1 finapp1.o 

To compile the program just type
make

To remove the compiled program and object type
make clean

*****

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.