GNU C and C++ Compilers for Linux
The most important software-development tool in Linux is GCC — the GNU C and C++ compiler. In fact, GCC can compile three languages: C, C++, and Objective-C (a language that adds object-oriented programming capabilities to C).
You use the same gcc command to compile and link both C and C++ source files. The GCC compiler supports ANSI-standard C, making it easy to port any ANSI C program to Linux. In addition, if you’ve ever used a C compiler on other Unix systems, you should feel right at home with GCC.
Use the gcc command to invoke GCC. By default, when you use the gcc command on a source file, GCC preprocesses, compiles, and links to create an executable file. However, you can use GCC options to stop this process at an intermediate stage.
For example, you might invoke gcc by using the -c option to compile a source file and to generate an object file, but not to perform the link step.
Using GCC to compile and link a few C source files is easy. Suppose you want to compile and link a simple program made up of two source files. To accomplish this task, use the following program source code; the task that is stored in the file area.c computes the area of a circle whose radius is specified at the command line.
Compiling C++ programs
GNU CC is a combined C and C++ compiler, so the gcc command also can compile C++ source files. GCC uses the file extension to determine whether a file is C or C++. C files have a lowercase .c extension, whereas C++ files end with .C or .cpp.
Although the gcc command can compile a C++ file, that command doesn’t automatically link with various class libraries that C++ programs typically require. Compiling and linking a C++ program by using the g++ command is easy because it runs gcc with appropriate options.
Exploring GCC options
Here’s the basic syntax of the gcc command:
gcc options filenames
Each option starts with a hyphen (-) and usually has a long name, such as -funsigned-char or -finline-functions. Many commonly used options are short, however, such as -c, to compile only, and -g, to generate debugging information (needed to debug the program by using the GNU debugger, gdb).
You can view a summary of all GCC options by typing the following command in a terminal window:
Then you can browse through the commonly used GCC options. Usually, you don’t have to provide GCC options explicitly because the default settings are fine for most applications. This table lists some of the GCC options you may use.
|-ansi||Supports only ANSI-standard C syntax. (This option disables
some GNU C-specific features, such as the_asm and_typeof
keywords.) When used with g++, supports only ISO-standard C++.
|-c||Compiles and generates only the object file.|
|-DMACRO||Defines the macro with the string “1”
as its value.
|-DMACRO=DEFN||Defines the macro as DEFN, where
DEFN is some text string.
|-E||Runs only the C preprocessor.|
|-fallow-single-precision||Performs all math operations in single precision.|
|-fpcc-struct-return||Returns all struct and union values in memory, rather than in
registers. (Returning values this way is less efficient, but at
least it’s compatible with other compilers.)
|-fPIC||Generates position-independent code (PIC) suitable for use in a
|-freg-struct-return||When possible, returns struct and
union values registers.
|-g||Generates debugging information. (The GNU debugger can use this
|-I DIRECTORY||Searches the specified directory for files that you include by
using the #include preprocessor
|-L DIRECTORY||Searches the specified directory for libraries.|
|-l LIBRARY||Searches the specified library when linking.|
|-mcpu=cputype||Optimizes code for a specific processor. (cputype can take many different values —
some common ones are i386, i486, i586, i686,
pentium, pentiumpro, pentium2, pentium3, pentium4.)
|-o FILE||Generates the specified output file (used to designate the name
of an executable file).
|-00 (two zeros)||Does not optimize.|
|-O or -O1 (letter O)||Optimizes the generated code.|
|-O2 (letter O)||Optimizes even more.|
|-O3 (letter O)||Performs optimizations beyond those done for -O2|
|-Os (letter O)||Optimizes for size (to reduce the total amount of code).|
|-pedantic||Generates errors if any non-ANSI-standard extensions are
|-pg||Adds extra code to the program so that, when run, this program
generates information that the gprof
program can use to display timing details for various parts of the
|-shared||Generates a shared object file (typically used to create a
|-UMACRO||Undefines the specified macros.|
|-v||Displays the GCC version number.|
|-w||Doesn’t generate warning messages.|
|-W1, OPTION||Passes the OPTION string (containing multiple
comma-separated options) to the linker. To create a shared library
named libXXX.so.1, for example, use the
following flag: -Wl,-soname,libXXX.so.1.