Friday, July 10, 2009

Compilation in C Programming language | C Compilation

Source codes for C programs are written as ASCII text. They must be compiled and linked to produce executable programs. The executable files are what you actually run on UNIX.

The compilation of code involves several steps:

  • parsing of the statements for syntax
  • translation of the statements into machine language
  • setting up the addresses of all variables
  • optimization of the code (if desired)
In most modern computers, various languages, e.g. Fortran and C, have distinct syntax handlers, but share a common translator into assembly language, and a common optimization engine.

The linking step assembles the various routines produced by the compiler during the compilation step, and resolves missing calls to either language-specific libraries or system-wide functions.

We will use the GNU C compiler for this course because it is (a) free and (b) more efficient than the Sun equivalent. This compiler is invoked via the gcc command. Typing

 gcc fname.c
will compile the file fname.c, linked it to the default libraries and produce an executable file called by default a.out.

Several parameters may be used to modify the default in the compilation. Some of these are:

 -o out_file        specify the output file (executable, or
binary) to be out_file

-O, -O1, -O2, -O3 specify optimization level

-c fname.c indicates that you only want to compile
the file
fname.c, in which case
the output file will be
fname.o

-lname links with the library
libname.a

-Ldirectory directs gcc to look for libraries in
directory, in addition to the
standard system library path

We strongly advise that you omit optimization until you are sure a program works. Optimizers tend to change the code around internally for the sake of efficiency, and they can make it extremely difficult to find errors in a complicated program. In addition, many have been known to introduce bugs of their own...

We also maintain the following aliases on newton to facilitate your compilations:

 C fname    will compile and link the C program in
the file
fname.c with the standard
C mathematics library, placing the executable
in
fname

Cgfx will compile the code in
fname.c,
then link it with the standard C mathematics
library and the
xutility graphics
library.

This library contains (among other things)
a set of plotting functions written by Biao
Lu (a former graduate student who left us
in 1994) which form the basis for much of
the graphics we will need to do during the
quarter.

No comments:

Blog List