CP264 : Notes - C Compilation


Characteristics of C


Uses of C


C program structure

C source code

[pre-processor statements]
[declare global variables and constants]
[declare function prototypes]

int main(argument(s)) {
    [program statements]
    return 0;
}

[implementation of functions]

Generally, header information (pre-processor statements, global variables, constants, and function prototypes) are put into separate header (.h) files, while the function implementations are placed in one or more source code (.c) files. If source code has been compiled into a library, the associated header files describe access to the library functions.

First C Program

// This is my first program in C
#include <stdio.h>

int main(void) {
   printf("Hello world\n");
   return 0;
}

File Name Extension Convention

.c
C language source file
.h
C language header file
.o
object file
.s
assembly language source file
.exe
executable (for Windows)
.out
executable (for Unix/Linux)
.lib
static library (for Windows)
.a
static library (for Unix/Linux)
.dll
dynamic library (for Windows)
.so
dynamic library (for Unix/Linux)

Compilation, Linking, and Execution

compilation (29K)

The Steps

  1. pre-processing
  2. compilation
  3. assembly
  4. linking
  5. generation of an executable program file (a program in machine code)

Compiling

We use GNU (GNU's Not Unix) C compiler

Simple Command Line Example:

gcc helloworld.c
  1. Pre-Processing

    Resolve lines Pre-processor directives (lines that start with #), are resolved. Examples:

    #include <stdio.h>
    
    #define STACK_INIT 8
    
  2. Compilation

    Convert the C program into an assembly program. (This generally does not create a file.)

  3. Assembly

    Convert the assembly code to binary format, called an object file. (This creates a .o file.)

  4. Linking

    Link the object file with other library files

  5. Executable Generation

    From the linked code, create an executable program file. (.exe or .out files.)

The resulting executable program can then be loaded into memory and executed.

Compiler and Linker Options

There are a large number of compiler and linker options available. These options give the compiler / linker instructions on how to compile and link the source code. They can be set on the command line, in make files (files that you can use to set instructions for the compiler / linker), or in IDE (i.e. Eclipse) settings. The following are a few examples:

-c

(compile) suppresses linking step of compilation, but generates an object file

gcc -c helloworld.c

generates helloworld.o

-g

(gdb) embeds diagnostic (debugging) info in the object file. Still creates an executable, but allows you to walk through the executable with a debugger program.

gcc -g helloworld.c

generates a.exe

-o object

(output) names the executable program from linking instead of the default name

gcc -o hello helloworld.c

generates an executable file named hello.exe

-Wlevel

(warnings level) give all compiling warnings

gcc -Wall helloworld.c

Show all warnings.

-Idirectory

(include file path) give the list of directories for directive include files, if these files are not in the current directory

-larg

(libraries) Link with the library file named libarg.lib . By default search /lib and /usr/lib (or other default library directories) for this library

-Ldirectory

(Library search path) Search for the library libarg.lib from the above option in the directory directory.

-std=standard

(standard) Set the language standard for compilation.

gcc -std=c99 helloworld.c

Compile to the C99 standard