gcc, the GNU Compiler Collection

Red Hat Enterprise Linux 3: Developer Tools Guide
Prev | Chapter 2. Basic Principles of the Tools | Next

# 2.4. `gcc`, the GNU Compiler Collection

The GNU Compiler Collection (GCC), is a complete set of tools for compiling programs written in C, C++, Objective C, or languages for which you have installed front-ends. The GNU compiler uses the following utilities:

- `cpp` GNU preprocessor that processes all the header files and macros that your target requires (refer to Section 2.5 `cpp`, the GNU Preprocessor).

- `as` GNU assembler that produces binary code from assembly language code and puts it in an object file (refer to Section 2.6 `as`, the GNU Assembler).

- `ld` GNU linker that binds the code to addresses, links the startup file and libraries to an object file, and then produces an executable binary image (refer to Section 2.7 ld, the GNU Linker).

To invoke the compiler, type:

    gcc option, option...

Each ``option`` signifies input that allows you to control how the program compiles (for example, you can interrupt a compile process at intermediate stages). Use commas to separate options. There are many options available that provide specific types of compiled output, some for preprocessing, others controlling assembly, linking, optimization, debugging, and still others for target-specific functions. For instance, call the compiler with a `-v` option to refer to precisely which options are in use for each compilation pass.

`gcc` implicitly recognizes the following file extensions:

- `.c` (for C source code that must be preprocessed)

- `.C` (for C++ source code that must be preprocessed)

- `.s` (for assembler code)

- `.S` (for assembler code that must be preprocessed).

When referring to C++ compilation, `g++` is its customary name. Because there is only one compiler, it is also accurate to use the `gcc` call, no matter what the language context. The `g++` distinction is more useful when the emphasis is on compiling C++ programs, with the GNU compiler acting not merely as a preprocessor, but building object code directly from your C++ program source.

There is no intermediate C version of the program; avoiding an intermediate C representation of the program means that you get better object code and better debugging information. The GNU debugger works with this information in the object code to give you comprehensive C++ source-level editing capabilities.

When you compile C or C++ programs, the compiler inserts a call at the beginning of `main` to a `_ _main` support subroutine. To avoid linking to the standard libraries, specify the `-nostdlib` option. (Including `-lgcc` at the end of your compiler command line resolves this reference and links only with the compiler support library `libgcc.a`; ending your command's input with it ensures that you get a chance to link first with any of your own special libraries).

`_ _main` is the initialization routine for C++ constructors. Because GNU C is meant to interoperate with GNU C++, even C programs must have this call; otherwise, C++ object files linked with a C `main` might fail.

Compilation can involve up to four stages, always in the following order:

- preprocessing

- compiling

- assembling

- linking

The first three stages apply to an individual source file: preprocessing establishes the type of source code to process, compiling produces an object file, assembling establishes the syntax that the compiler expects for symbols, constants, expressions and the general directives. The last stage, linking, completes the compilation process, combining all object files (newly compiled, and those specified as input) into an executable file.

For more information on the GNU compiler and its options, refer to Using the GNU Compiler Collection (GCC).

Prev | Home | Next
Auxiliary Development Tools | Up | `cpp`, the GNU Preprocessor