How to Compile C and C++ Programs on Unix Systems


Compiling A Single-Source "C" Program


The easiest case of compilation is when you have all your source code set in a single file. Let's assume we have a single source file named "hello_world.c" to compile. Before you start compiling the program, make sure that you are in the right directory on Sparky or Compserv and that the source code files are in the directory. Use the command "pwd" to print the current working directory. This will enable you to work out where you are in relation to the whole file-system. Use the command "gcc" to compile and link your program

% gcc hello_world.c[1]

The compiler might show its messages (errors, warnings, etc.) differently, but in all cases, you'll get a file "a.out" as a result, if the compilation completed successfully. Suppose that you want the resulting program to be called "hello_world". In that case, you could use the following line to compile it:

% gcc -o hello_world hello_world.c

The '-o' flag simply name the resulting executable file as specified.


Running the Executable Program


Once we have compiled and linked the program, we can run it by simply typing its name:


% hello_world


However, this requires that directory in which the executable file resides be in our PATH (which is a variable telling our Unix shell where to look for programs we're trying to run). In many cases, this directory is not placed in our PATH. So, try the following command instead:

% ./hello_world


By putting './' in front of the file name, we explicitly tell our Unix shell that we want to run the program from the current directory.


Compiling A Single-Source "C++" Program


Now that we saw how to compile C programs, the transition to C++ programs is rather simple. All we need to do is to use a C++ compiler, in place of the C compiler we used so far. So, if our program source is in a file named "" ('cc' is a suffix for C++ code. Some programmers prefer a suffix of 'C' or 'cpp' for C++ code), we will use a command such as the following:

% g++ -o hello_world


Note, however, before you try to build the program on Sparky, you need to set the path to the dynamic libraries. If you are using SSH Secure Shell Client, then use the following command to do so:


% setenv LD_RUN_PATH  /usr/local/lib


After you do that, compile your code and run the program, everything should be fine.

Compiling A Multi-Source "C" Program


There are two possible ways to compile a multi-source C program. The first is to use a single command line to compile all the files. Suppose that we have a program whose source is found in files main.c, a.c and b.c We could compile it this way:

% gcc -o hello_world main.c a.c b.c

This will cause the compiler to compile each of the given files separately, and then link them all together to one executable file named "hello_world". Two comments about this program:



The problem with this way of compilation is that even if we only make a change in one of the source files, all of them will be re-compiled when we run the compiler again. In order to overcome this limitation, we could divide the compilation process into two phases - compiling, and linking. Lets first see how this is done, and then explain:


% gcc -c

% gcc -c a.c

% gcc -c b.c

% gcc -o hello_world main.o a.o b.o

The first 3 commands have each taken one source file, and compiled it into something called "object file", with the same names, but with a '.o' suffix. It is the '-c' flag that tells the compiler only to create an object file, and not to generate a final executable file just yet. The object file contains the code for the source file in machine language, but with some unresolved symbols. After creating the 3 object files, we use the 4th command to link the 3 object files into one program. The linker (which is invoked by the compiler now) takes all the symbols from the 3 object files, and links them together. Further more, the linker also links the standard C library into the program, in this case, to resolve the "printf" symbol properly.


To see why this complexity actually helps us, we should note that normally the linking phase is much faster than the compilation phase. This is especially true when doing optimizations, since that step is done before linking. Now, lets assume we change the source file "a.c", and we want to re-compile the program. We'll only need now two commands:


% gcc -c a.c

% gcc -o hello_world main.o a.o b.o


In our small example, it's hard to notice the speed-up, but in a case of having few tens of files each containing a few hundred lines of source-code, the time saving is significant; not to mention even larger projects.




Q: How do I link my program with the math library?

A: Suppose you have a file called 'code.c' which includes 'math.h'. Use the following command to link your program with the math library:


% gcc -lm code.c







[1] "gcc" is the command for GNU compiler. If you're using a Solaris system, you might use "acc".