Next: , Previous: , Up: Top   [Contents][Index]


29 Compilation

Early in the manual we explained how to compile a simple C program that consists of a single source file (see Compile Example). However, we handle only short programs that way. A typical C program consists of many source files, each of which is a separate compilation module—meaning that it has to be compiled separately.

The full details of how to compile with GCC are documented in xxxx. Here we give only a simple introduction.

These are the commands to compile two compilation modules, foo.c and bar.c, with a command for each module:

gcc -c -O -g foo.c
gcc -c -O -g bar.c

In these commands, -g says to generate debugging information, -O says to do some optimization, and -c says to put the compiled code for that module into a corresponding object file and go no further. The object file for foo.c is called foo.o, and so on.

If you wish, you can specify the additional options -Wformat -Wparenthesis -Wstrict-prototypes, which request additional warnings.

One reason to divide a large program into multiple compilation modules is to control how each module can access the internals of the others. When a module declares a function or variable extern, other modules can access it. The other functions and variables in a module can’t be accessed from outside that module.

The other reason for using multiple modules is so that changing one source file does not require recompiling all of them in order to try the modified program. Dividing a large program into many substantial modules in this way typically makes recompilation much faster.

After you compile all the program’s modules, in order to run the program you must link the object files into a combined executable, like this:

gcc -o foo foo.o bar.o

In this command, -o foo species the file name for the executable file, and the other arguments are the object files to link. Always specify the executable file name in a command that generates one.

Normally we don’t run any of these commands directly. Instead we write a set of make rules for the program, then use the make program to recompile only the source files that need to be recompiled.


Next: , Previous: , Up: Top   [Contents][Index]