-->
Previous Table of Contents Next


GCC Options

This section describes some of the GCC options that are most commonly used. First we’ll talk about some of the options that can be used both with C and C++ and then talk about C++ specific options. Any of the compiler options that you use with C you can use with C++ as well, but some of them may not make any sense in the context of a C++ compile. If you specify options that don’t make sense, the compiler just ignores them.


Tip:  
When you are compiling C++ programs, it is easiest to use the g++ script. This sets all the default C++ options so you don’t have to.

A great number of compiler options can be passed to GCC. Many of these options are specific to a certain hardware platform or are for making fine-tuning adjustments to the code that is produced. You will probably never use any of these kinds of options. The options covered in this chapter are those that you will use on a regular basis.

Many of the GCC options consist of more than one character. For this reason, you must specify each option with its own hyphen and not group options after a single hyphen as you can with most Linux commands.

When you compile a program using GCC without any command-line options, it creates an executable file (assuming that the compile is successful) and calls it a.out. For example, the following command creates a file named a.out in the current directory:


gcc test.C

To specify a name other than a.out for the executable file, you can use the -o compiler option. For example, to compile a C++ program file named count.C (the capital C is used to show C++ code, as opposed to a small c for C code) into an executable file named count, type the following command:


gcc -o count count.C


Warning:  
Don’t put a space after the -o option!

When you are using the -o option, the executable filename must occur directly after the -o on the command line.


Other compiler options allow you to specify how far you want the compile to proceed. The -c option tells GCC to compile the code into object code and skip the assembly and linking stages of the compile. This option is used quite often because it makes the compilation of multifile C++ programs faster and easier to manage. Object code files created by GCC have an .o extension by default.

The -S compiler option tells GCC to stop the compile after it has generated the assembler files for the C code. Assembler files generated by GCC have an .s extension by default. The -E option instructs the compiler to perform only the preprocessing compiler stage on the input files. When this option is used, the output from the preprocessor is sent to the standard output rather than being stored in a file.

Debugging and Profiling Options

GCC supports several debugging and profiling options. Of these options, the two that you are most likely to use for C++ programs are the -gstabs+ option and the -pg option.

The -gstabs+ option tells GCC to produce stabs format debugging information that the GNU debugger (gdb) can use to help you debug your program. For more information on debugging your C++ programs, see the “Debugging C++ Applications” section later in this chapter.

The -pg option tells GCC to add extra code to your program that will, when executed, generate profile information that can be used by the gprof program to display timing information about your program. For more information on gprof, refer to the “gprof” section in Chapter 26, “Programming in C.”

GCC C++ Specific Options

The GCC options that control how a C++ program is compiled are listed in Table 27.1.

Table 27.1. GCC options.

Option Meaning

-fall-virtual Treats all possible member functions as virtual. This applies to all functions except for constructor functions and new or delete member functions.
-fdollars-in-identifiers Accepts $ in identifiers. You can also prohibit the use of $ in identifiers by using the -fno-dollars-in-identifiers option.
-felide-constructors Tells the compiler to leave out constructors whenever possible.
-fenum-int-equiv Permits implicit conversion of int to enumeration types.
-fexternal-templates Produces smaller code for template declarations. This is done by having the compiler generate only a single copy of each template function where it is defined.
-fmemorize-lookups Uses heuristics to compile faster. These heuristics are not enabled by default because they are effective only for certain input files.
-fno-strict-prototype Treats a function declaration with no arguments the same way that C would treat it. This means that the compiler treats a function prototype that has no arguments as a function that will accept an unknown number of arguments.
-fno-null-objects Assumes that objects reached through references are not null.
-fsave-memorized Same as -fmemorize-lookups.
-fthis-is-variable Permits assignment to “this.”
-nostdinc++ Does not search for header files in the standard directories specific to C++.
-traditional This option has the same effect as -fthis-is-variable.
-fno-default-inline Does not assume that functions defined within a class scope are inline functions.
-wenum-clash Warns about conversion between different enumeration types.
-woverloaded-virtual Warns when derived class function declaration may be an error in defining a virtual function. When you define a virtual function in a derived class, it must have the same signature as the function in the base class. This option tells the compiler to warn you if you have defined a function that has the same name and a different signature as a function that is defined in one of the base classes.
-wtemplate-debugging If you are using templates, this option warns you if debugging is not yet available.
+eN Controls how virtual function definitions are used.
-gstabs+ Tells the compiler to generate debugging information in stabs format, using GNU extensions understood only by the GNU debugger. The extra information produced by this option is necessary to ensure that gdb handles C++ programs properly.


Previous Table of Contents Next