Compiling Programs
This document contains tips for compiling C programs on Unix. It is NOT a programming tutorial.
Contents
Which Compiler to Use
For compiling C programs on sun machines, CISE users have access to the standard Sun cc and gcc (GNU Compiler Collection). Both compilers are well tested and well established and confirm to standards.
For compiling C++ programs on sun machines, you can use CC or g++.
On linux machines, only the GNU compilers are avaialble.
Common Compiler Usage
Most of the common command line options for cc and gcc are identical. Likewise, the command line options for CC and g++ are identical (and many of the options for all four are identical).
- Simple compilation
- Let's say you want to compile a program named hello.c, you can invoke
the compiler with:
gcc hello.c
This creates an executable named a.out which can be invoked as:
./a.out
If you want to name your executable different use the
-ooption as:gcc -o hello hello.c
to create an executable named hello.
The other compilers can be invoked in the same way, and the -o option is available for all four.
- Specifying include paths
- Most of the include files are in either /usr/include or /usr/local/include,
and most often they will be found automatically by the compiler. If you get
a message similar to the following (when using cc/CC):
line 123: cannot find include file: <foo.h>
or (when using gcc/g++):123: foo.h: No such file or directory
it means that the include file is not in a path that is automatically being searched by the compiler. To fix this, you must first find the include file, and then tell the compiler where to search for it.
Look in the directories above to see if it is there (occasionally, you have to explicitely tell the compilers to look there). Also, look in the current directory, or any other places where the include file might reasonably be.
Once you have determined the directory where the include file lives, you can use the -I option to specify include path:
gcc -I/path/to/my/include/dir hello.c
The -I option applies to all compilers.
- Linking libraries
- Whenever you use a function from a library in your program, you must link
against the appropriate library. For example, to link against the pthread
library, you can use the -l option to do so:
gcc mythread_program.c -lpthreadIn the above command, your program is linked with pthread library which contains code for thread functions.
Most libraries are in /usr/lib or /usr/local/lib.
If you are unsure about the linking options for the functions you are using, read the first part of the man page for the function carefully. It usually looks like this
cc file ... -lpthread [ library... ] #include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine, void*), void *arg);This means that you need to add the -lpthread to the compile line, and you need to include pthread.h in the program.
cc/CC Specific Usage
gcc/g++ Specific Usage
Debugging
To debug your programs with gdb (GNU debugger) or dbx (Sun's debugger), you have to compile your programs with -goption as
gcc -g hello.c
See the debugging tuotrial for more deatils on using gdb, dbx and ddd (A graphical front end for the debuggers).
Common problems
If your program fails with errors like
% ./a.out Segmentation fault (core dumped)
or
% ./a.out Bus error(core dumped)
it usually implies memory overrruns in your program. See the debugging tutorial for more details on debugging and making use of the core file.