Next 16.C: Programming Courtesy  Up 16: Programming  Prev 16: Programming  Contents


§ 16.B: Compiling C Programs

Once you have gone through the trouble of writing a program, you probably want to have it run. The most common Unix C compiler is called cc. The most basic use of cc is demonstrated as follows:

> cd ~/development/prog
> cc myprog.c

where myprog.c is the name of the program you wish to compile. This command produces a file called a.out which is the executable program. To run it, you just type a.out and then enter:

> a.out

a.out is not a very descriptive name. Fortunately, it is possible for the compiler to give it a new name when it is done. This is done with the -o flag. You use it after the compile line shown above, followed by the name you want the executable program to have:
> cc myprog.c -o it-runs

This compiles your C program into an executable called it-runs:

> it-runs

Many programs use the mathematical functions found in the C math library. Unfortunately, the math library is not part of the standard library that automatically gets compiled with your program, and the compiler isn't smart enough to figure out when it is needed. So, you have to let the compiler know that there is another library to include. You do this by using the -l flag, followed immediately (no spaces) by the reference name of the required library. This reference name is m for the math library, so a program which uses the math library would be compiled this way:
> cc big-math.c -o the-answer -lm

This compiles big-math.c into an executable program called the-answer, telling the compiler that it also needs to use the math library. To run the program:
> the-answer

For scientific programs, LAPACK is another commonly used library. For a tutorial on how to use LAPACK routines, look at the PH 465 LAPACK tutorial.

Another readily available compiler is gcc, which is used the same way, but, is also capable of compiling C++ programs.

Next 16.C: Programming Courtesy  Up 16: Programming  Prev 16: Programming  Contents

Comments and questions to