Compiling C Program in Linux Part 1
Most of Linux are written in C. To compile C program we need a minimum of the follow packages:
- Gcc – the compiler for C program
- Glibc – contains standard C libraries
- Glibc-devel – contain standard headers file
- Binutils – contains various utilities such as assembler and linker
Writing C Program
You can use many tools in write C code. The most popular tool is vim and gedit. Listed below is a list of tools.
GUI (Graphic User Interface) Editing Tools
- gedit
- geany
- Eclipse
Command Line Editing Tools
- vim
- vi
- Emacs
A Simple C Program
Listed below is a simple C program called sick.c
#include <stdio.h> int main() { printf ("Sick of printing hello world!\n"); return 0; }
To compile the program
$gcc -Wall sick.c -o verysick
The option -Wall display all possible warning (-W is for warning and all is to display all possible warnings) 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
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 ( ) { 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 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. -l is the option for library. The math library is libm.a. You can omit the prefix lib and .a.
To run the program indicate the complete path or
$./tvm
*****
Comments
Post a Comment