Writing and Compiling C Program in Linux Part 3: Using Makefile

March 19, 2011

Compiling C Program using Make

  • Using the example in Part 2, 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

  • The file is usually called Makefile
  • The Makefile of previous tvm program will be as follows:
#Creating object
tvm2: tvm2.o finlib.o
gcc tvm2.o finlib.o -lm -o tvm2
tvm2.o: tvm2.c fin.h
gcc -c tvm2.c
finlib.o: finfn.c fin.h
gcc -c finfn.c -o finlib.o
clean:
rm -f tvm2 tvm2.o finlib.o
  • This is the more explicit way to instruct the make program how to compile 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.
  • See the example below:
CFLAGS=-Wall
LDFLAGS=-lm
#Creating object
tvm: tvm.o finfn.o fin.h
clean:
rm -f tvm tvm.o finfn.o
  • CFLAGS refers to compile 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.
  • Since the custom library need not compile every time, we can include the main C program and the object file of the library.
  • The Makefile will be slightly different, as follows:
CFLAGS=-Wall
LDFLAGS=-lm
#Creating object
tvm: tvm.o finlib.o fin.h
clean:
rm -f tvm tvm.o
  • We don’t supply finfn.c but we supply a precompiled object file finlib.o.

Writing and Compiling C Program in Linux Part 2: Using Custom Library

March 18, 2011
  • Instead of building custom function in a C program, we can include any custom function into a custom library. In this way, we could reuse the custom function between different program.
  • First, we maintain the same main(), but we remove all custom financial functions. Instead we included a statement included “fin.h” so that the compiler will search for the header file.
#include <stdio.h>
#include "fin.h"
int main (void)
{
double myrate;
int myterm;
double myprincipal;
double myreturn;
printf ("Please enter the following:\n");
printf ("Rate of return:(In decimal 50%% is 0.5)");
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;
}
  • Next, we create a header file (“fin.h”) that will include all financial functions in the future.
double tvm (double rate, int terms, double principal);
  • Finally, we create a new program call finfn.c (as in financial function). The custom function will be place here.
#include <math.h>
#include "fin.h"
double tvm (double rate, int terms, double principal)
{
double dn;
double pp;
dn = terms;
pp = pow (1+rate, dn);
return principal * pp;
}
  • The purpose splitting the program is such that tvm function can be reuse as a custom library in another program.
  • We can compile all the program #gcc -Wall tvm.c finfn.c -o tvm –lm
  • However, we don’t have to compile finfn.c every time of there is no changes.
  • One way is to create an object file during compilation instead of create executable.
  • We compile finfn.c first: #gcc -Wall -c finfn.c -o finlib.o
  • With the object file available, we can create another program tvm2 using the same tvm function:
#include <stdio.h>
#include "fin.h"
int main (void)
{
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;
}
  • Then we compile tvm2.c first before linking:
#gcc -Wall -c tvm2.c -o tvm2.o
  • We can link both program as follows:
#gcc tvm.o finlib.o -lm -o tvm
#gcc tvm2.o finlib.o -lm -o tvm2
  • In the example, we compile 2 programs using the custom function in one object file.
  • Alternatively, we can compile straight from tvm2.c to executable like below:
#gcc -Wall tvm2.c finlib.o -lm -o tvm2

Writing and Compiling C Program in Linux GCC Part 1

March 17, 2011
  • GCC compiler is usually installed together with your Linux. If not, use the command $sudo apt-get install gcc (for Ubuntu user) and #yum install gcc (for Fedora, Redhat user). For Fedora user, you need to be in su mode before installation.
  • Below is a simple C program called sick.c
#include <stdio.h>
int main(void)
{
printf ("Sick of printing hello!\n");
return 0;
}
  • To compile the program #gcc -Wall sick.c -o verysick
  • The option -Wall display all possible Warning and –o indicate to the compiler that user want the output to be name after the -o option.
  • To run the program indicate the complete path or $./verysick

Writing and Compiling C Program using Math Library

  • Below is a more complex C program called tvm.c
#include <stdio.h>
#include <math.h>
double tvm (double rate, int terms, double principal);
int main (void)
{
double myrate;
int myterm;
double myprincipal;
double myreturn;
printf ("Please enter the following:\n");
printf ("Rate of return:(In decimal 50%% is 0.5) ");
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 compile the program #gcc -Wall tvm.c -lm -o tvm
  • The additional option -lm is required so that the compiler will search for the math library in reference to including math.h.