delorie.com/djgpp/doc/ug/basics/compiling.html   search  
Guide: Compiling Programs

Being of a Unix origin, GCC has a somewhat different flavor of command-line syntax and its peculiar compilation and link algorithms. It also has a plethora of optional switches, some of them obscure or semi-documented. These are known to confuse users, especially those who had previous experience with DOS-based C compilers.

As GCC is a compiler, it's primary purpose is to convert source code that you write into programs the computer can run. To do this, it has many resources it calls upon. First and foremost, it reads your source files. Based on the file extension that you gave your program, it selects the appropriate commands it needs to run to turn the source you gave it into the output file you specified. Remember, gcc is primarily a Unix compiler, so it assumes that the file names you are giving it are case sensitive! Here is a list of file extensions and what gcc does with them.

.c (lower case)
Standard C source. Note that .C (upper case) is not a C source - it's a C++ source. C source files are preprocessed (through cpp) for #includes and #defines, then compiled by cc1.

.m
Objective C files. Preprocessed then compiled by cc1obj. You need obc*b.zip to compile these.

.h
These are include files. While gcc won't compile them, it does recognize the extension so that it can prevent you from trying to compile them.

.i
These are C sources that have already been preprocessed. gcc won't preprocess them again, but it will compile them with cc1.

.s (lower case)
Assembler source files. These are passed directly to as.

.S (upper case)
Also assembler source files, but these are preprocessed through cpp before being assembled. This way, you can use #include and #define. Remember that gcc is case sensitive! If you use a Makefile, you're probably best off if you explicitely list your assembler sources so that you don't rely on which case make defaults to.

.cc .cxx .cpp .c++ .C (upper case)
C++ sources. These are preprocessed with cpp and then compiled with cc1plus. You need gpp*b.zip.

.ii
C++ sources that have been preprocessed. These are sent directly to cc1plus.

.ads .ada
Ada sources. These are preprocessed and compiled with the ada compiler, gnat1. You need to install GNU Ada to compile these.

.o
Object files. These are the output of the compiler/assembler. If you tell gcc to just compile (by using the -c option), this is what it produces. You can link multiple objects together to produce an executable.

.a
Libraries. These contain multiple objects, which gcc may link into your program to provide functions you've called.

Here's a summary of what gcc does for you:

flow diagram

You probably noticed that gcc can produce a number of different types of output files as well. The -E, -S, and -c options tell gcc to stop the process at the indicated point and produce output. Note: Output from the -E option is printed to the screen, unlike the other options that save the output in a file. You can redirect the output to a file if you want.

If you don't specify any output options, by default gcc creates an output file called a.exe. This is different from most dos compilers, which name the program after the first source or object it finds. You may specify an alternate output file with the -o option (which also works in conjunction with -c or -S), like this:

The above example is also the simplest way to compile a C source file into a dos executable. If your program consists of multiple sources, you can compile them all at once:

You can also compile each separately and link them into a program:

Separate compilation is faster when you're debugging your program, because you only have to recompile the sources that you modify. When you use make to manage your project, it keeps track of this for you and only recompiles the modules you need.


  webmaster     delorie software   privacy  
  Copyright © 1997     Updated Apr 1997