C Program Compilation
C Program Compilation
/*
* File: hello.c
* -------------
* This simple C program prints out the text "Hello world!".
*
* COS315 Operating Systems
* 10 Mar 2003
*
*/
#include<stdio.h>
int main(void) {
printf("Hello world!\n");
}
To compile this program, we will be using the gcc compiler in Linux, which stands for “Gnu
Compiler Collection”, and it is used as follows:
$ gcc hello.c
This creates an executable called a.out. To run the executable, you would type
$ ./a.out
Hello world!
If we wanted to create an executable that is named something other than a.out, we would
use the -o option in the form
$ ./hello
Hello world!
In what follows we will go over the details of what actually happens when you invoke the C
compiler gcc.
Handout 6 10/03/03 2
The preprocessor
The main function of the C preprocessor is to remove comments from the source code and
interpret preprocessor directives which are given by the statements that begin with #. In our
simple hello.c code, the preprocessor would strip the source of the comments contained
within the /*...*/ and would include the file called stdio.h, which contains the standard
input/output functions that are usually called within a C program. The #include statement
can either be called with
#include<file.h>
or with
#include ‘‘file.h’’
The first method tells the preprocessor to look for the file in the standard include directories,
which for Linux are in /usr/include. The second method, which uses the quotes, tells the
preprocessor that the file to be included is in the local directory. We will go into more detail
on different preprocessor directives as we look at different facets of the C programming
language.
Handout 6 10/03/03 3
Linking
The object file hello.o contains a binary version of the machine language that was created
from your source code hello.c. In order to create the executable hello or a.out, you need
to use the linker to process your main function and any possible input arguments you might
use, and link your program with other programs that contain functions that your program
uses. In this very simple example, we used the printf function. The printf function is
a standard function that is provided by the C compiler that your current object file knows
nothing about. In order to use this function, we need to use the linker in order to link our
program with the precompiled libraries provided to us by the C compiler. The linker links
other precompiled object files or libraries together and creates the executable hello. When
you type
the gcc compiler creates an object file and does the linking for you. However, when you use
the -c option, you create an object file called hello.o. In order to link this object file and
create the executable, you can do the linking yourself by again using the gcc compiler, but
this time you provide the object files as the command line arguments rather than the source
codes. Then you would type
When gcc sees object files, it invokes the linker automatically and links the necessary files
to create the hello executable.
$ man 3 sqrt
This give us information about the function and the appropriate header file to use
NAME
sqrt - square root function
Handout 6 10/03/03 5
SYNOPSIS
#include <math.h>
// Program math.c
int main(void) {
sqrt(7*M_PI);
}
$ gcc -c math.c
math.c: In function ‘main’:
math.c:2: ‘M_PI’ undeclared (first use in this function)
It is important to understand that this error is produced by the compiler and not by the
linker. The compiler is attempting to create the object file math.o but can’t in this case
because there is no definition of the variable M_PI. In order for the compiler to know what
the value of M_PI is, we need to include the header file math.h. If we do so, and our code
looks like
// Program math.c
#include <math.h>
int main(void) {
sqrt(7*M_PI);
}
then we will get no errors when we try to create the object file with
$ gcc -c math.c
But remember, even by including the header file we still need to tell the linker about the
library that contains the sqrt function if we want to create an executable.