UH Navigation bar CS VNET NSM UH SEARCH

Computer Science Department Unix Primer

Compilers

The default compiler command for any Unix system is "cc". On some systems, this is the actual name of the compiler installed on the system, but on other systems, Linux in particular, cc is just a link to another compiler. The problem here is that not all compilers work alike. For the purposes of this tutorial, we will use the GNU Project's free compiler, GCC.

GCC is the default compiler for Linux systems, and most systems administrators will install it on other versions of unix. The command to call the compiler is "gcc" for C programs, g++ for C++ programs, and g77 for fortran programs. (For Bayou users, the proper C++ compiler is cxx.)

Say we have a simple "hello world" program called hello.c. We type:

$ gcc hello.c

to compile the program. The executable produced by this is "a.out" in the current directory. This is the default name for executables. This could cause a problem if you were to compile more than one program in each directory, so our first gcc option will be for setting the output name to something better.

In Unix, executables typically do not have extensions (like .exe, .com, or .bat). Following standard conventions, we will call our hello world program "hello".

$ gcc hello.c -o hello

Interestingly enough, this command matches the default rule for the make program which will be outlined later. Thus, you could also type:

$ make hello
cc hello.c -o hello

The only problem here, is that the makefile uses "cc" instead of "gcc", so it is possible that your code may behave differently between various versions of Unix.

The man page for gcc is quite extensive, so there are a few useful flags we'll point out here

-I directory Search directory for include files.
-L directory Search directory for library files.
-l library Link in library after compilation
-g Generate debugging information in the executable. (Necessary for using the debugger with your executable)
-O Automatically optimize your code by playing various compiler tricks. This option should not be used with -g because optimized code won't exactly match your source code. You can choose the level of optimization in gcc by specifying -O1, -O2, or -O3 with three being the highest.
University of Houston State of Texas Privacy and Policies Homeland Security Compact with Texans Reporting Copyright Infringement Contact U H Feedback Site Map Statewide Search U H System