Section 5.1. gcc Options


5.1. gcc Options

gcc has a multitude of command-line options. Fortunately, the set you usually need to know about is much smaller, and we cover those options here. Most of the options are generally the same or similar on other compilers, as well. gcc has voluminous documentation on its options available with info gcc.[2]

[2] man gcc has them too, but the man page is not updated as often as the Texinfo documentation.

-o filename

Specify the output file name. This is not usually needed if you are compiling to an object file because the default is to substitute filename.o for filename.c. However, if you are creating an executable, the default (for historical reasons) is to create an executable named a.out. It is also useful if you wish to put output files in another directory.


Compile, without linking, the source files specified on the command line, creating an object file for each source file. When using make, it is common to use one invocation of gcc per object file, because it is easy to see which file failed to compile if an error occurs. However, when you are typing commands by hand, it is commonly useful to specify many files in one invocation of gcc. In cases in which specifying many input files on the command line would be ambiguous, specify only one, or gcc may get confused. For instance, gcc -c -o a.o a.c b.c is equivalent to gcc -c -o a.o b.c.


Define a preprocessor macro on the command line. You may need to escape characters that are special to the shell. For instance, if you want to define a string, you will have to escape the " characters that delimit the string. Two common ways to do this are '-Dfoo="bar"' and -Dfoo=\"bar\". Note that the first works much better if there are any spaces in the string, because spaces are treated specially by the shell.


Prepend dir to the list of directories in which to search for include files.


Prepend dir to the list of directories in which to search for libraries. Unless otherwise instructed, gcc uses shared libraries in preference to static libraries.


Link against libfoo. Unless otherwise instructed, gcc links against shared libraries ( in preference to static libraries (ibfoo.a). The linker searches for functions in all the libraries listed, in the order in which they are listed, until each function is found.


Link against static libraries only. See Chapter 8 for details.

-g, -ggdb

Include debugging information. The -g option instructs gcc to include standard debugging information. The -ggdb option instructs gcc to include a large amount of information that only the gdb debugger is capable of understanding. Use -g if you have limited disk space, expect to use a debugger other than gdb, or are willing to trade away some functionality in gdb for linking speed. Use -ggdb if you need all the help you can get debugging, and gcc will pass more information to gdb.


Note that unlike most compilers, gcc is willing to include debugging information in optimized code. However, following the debugger as it traces through optimized code can be challenging the code path may jump around and completely miss sections of code you expected to be executed. It can also give you a better understanding of your code, and of how optimizing compilers change the way your code executes.

-O, -On

Instruct gcc to optimize your code. By default, gcc does a few optimizations; specifying a number (n) instructs gcc to optimize to a certain level. The most common optimization level is 2; with the standard version of gcc, 3 is currently the highest optimization level. Unless you wish to compile quickly at the expense of run-time speed, you expect to use a debugger on the output file, or you have found a bug in the optimizer, we recommend using -02 or -03 when compiling; -03 may increase the size of your application, so if this is a concern, we recommend that you test both ways. You may also consider -Os, which optimizes for minimum code size instead of for speed, if memory or disk space is at a premium for your application.


gcc does not implement inline functions unless at least minimal optimization (-0) has been enabled.


Support all standard ANSI (X3.159-1989) or the technically equivalent ISO (ISO/IEC 9899:1990) C programs (often abbreviated C89 or occasionally C90). Note that this does not enforce complete ANSI/ISO compliance. The -ansi option turns off gcc extensions that officially conflict with the ANSI/ISO standard. (Because many of these extensions are also supported by other C compilers, this is rarely a problem in practice.) It also defines the __STRICT_ANSI__ feature macro (as described on page 49), which the header files use to provide an ANSI/ISO- compliant environment.


Give all warnings and errors required by the ANSI/ISO C standard. This does not enforce absolute ANSI/ISO compliance.


Turn on all the generally useful warning messages that gcc can provide. It does not turn on options that are useful only in specific cases. This provides a similar level of detail to running the lint syntax checker on your source code. gcc allows you to turn each warning message on or off individually. The gcc manual lists all the warning messages.


    Linux Application Development
    Linux Application Development (paperback) (2nd Edition)
    ISBN: 0321563220
    EAN: 2147483647
    Year: 2003
    Pages: 168 © 2008-2017.
    If you may any questions please contact us: