Compiling C Programs

gcc is the "GNU" C Compiler, while cc can also be used. Below are several examples that show how to use gcc to compile C programs, which also to other C compilers as well

Example 1: Compiling a simple program

Consider the following example: Let "hello.c" be a file that contains the following C code.

#include 

void printmsg ()
{
	printf("Hello\n");
}

int main() 
{
	printmsg();
}

The standard way to compile this program is with the command

gcc hello.c -o hello 

This command compiles hello.c into an executable program named "hello" that you run by typing 'hello' at the command line. It does nothing more than print the word "hello" on the screen.

Alternatively, the above program could be compiled using the following two commands.

gcc -c hello.c
gcc hello.o -o hello 

The end result is the same, but this two-step method first compiles hello.c into a machine code file named "hello.o" and then links hello.o with some system libraries to produce the final program "hello". In fact the first method also does this two-stage process of compiling and linking, but the stages are done transparently, and the intermediate file "hello.o" is deleted in the process.


Frequently used compilation options

C compilers allow for many options for how to compile a program, and the examples below demonstrate how to use many of the more commonly used options. In each example, "hello.c" contains C source code for the executable "hello". In most cases options can be combined, although it is generally not useful to use "debugging" and "optimization" options together.

Compile hello.c so that hello contains symbolic information that enables it to be debugged with the gdb debugger.
gcc -g hello.c -o hello
Have the compiler generate many warnings about syntactically correct but questionable looking code. It is good practice to always use this option with gcc.
gcc -Wall hello.c -o hello
Generate symbolic information for gdb and many warning messages.
gcc -g -Wall hello.c -o hello
Generates optimized code.
gcc -O hello.c -o hello
Compile hello.c when it contains Xlib graphics routines.
gcc hello.c -o hello -lX11
Compile a C program that uses math functions such as "sqrt".
gcc hello.c -o hello -lm
Compile a C program with the "electric fence" library. This library, available on all the Linux machines, causes many incorrectly written programs to crash as soon as an error occurs. It is useful for debugging as the error location can be quickly determined using gdb. However, it should only be used for debugging as the executable hello will be much slower and use much more memory than usual.
gcc -g hello.c -o hello -lefence

Example 2: Compiling a program with multiple source files

If the source code is in several files, say "file1.c" and "file2.c", then they can be compiled into an executable program named "myprog" using the following command:
  gcc file1.c file2.c -o myprog 
To try this out use this code for "file1.c"
  void printmsg ();

  int main() 
  {
    printmsg();
  }
  
and this code for "file2.c"
  #include 

  void printmsg ()
  {
    printf("Hello\n");
  }
  
The same result can be achieved using the following three commands:
  gcc -c file1.c
  gcc -c file2.c
  gcc file1.o file2.o -o myprog 
The advantage of the second method is that it compiles each of the source files separately. If, for instance, the above commands were used to create "myprog", and "file1.c" was subsequently modified, then the following commands would correctly update "myprog".
  gcc -c file1.c
  gcc file1.o file2.o -o myprog 
Note that file2.c does not need to be recompiled, so the time required to rebuild myprog is shorter than if the first method for compiling myprog were used. When there are numerous source file, and a change is only made to one of them, the time savings can be significant. This process, though somewhat complicated, is generally handled automatically by a makefile.