Click on any of the 687 commands below to get a description and list of available options. All links in the command summaries point to the online version of the book on Safari Bookshelf.
GNU Compiler Collection. gcc, formerly known as the GNU C Compiler, compiles multiple languages (C, C++, Objective-C, Ada, FORTRAN, and Java) to machine code. Here we document its use to compile C, C++, or Objective-C code. gcc compiles one or more programming source files; for example, C source files (file.c), assembler source files (file.s), or preprocessed C source files (file.i). If the file suffix is not recognizable, assume that the file is an object file or library. gcc normally invokes the C preprocessor, compiles the process code to assemble language code, assembles it, and then links it with the link editor. This process can be stopped at one of these stages using the -c, -S, or -E option. The steps may also differ depending on the language being compiled. By default, output is placed in a.out. In some cases, gcc generates an object file having a .o suffix and a corresponding root name.
Preprocessor and linker options given on the gcc command line are passed on to these tools when they are run. These options are briefly described here, but some are more fully described under entries for cpp, as, and ld. The options that follow are divided into general, preprocessor, linker, and warning options. gcc accepts many system-specific options not covered here.
Note: gcc is the GNU form of cc; on most Linux systems, the command cc will invoke gcc. The command g++ will invoke gcc with the appropriate options for interpreting C++.
General options
-a
Provide profile information for basic blocks.
-aux-infofile
Print prototyped declarations and information on their origins to file.
-ansi
Enforce full ANSI conformance.
-bmachine
Compile for use on machine type.
-c
Create linkable object file for each source file, but do not call linker.
-dumpmachine
Print compiler's default target machine, then exit.
-dumpspecs
Print built-in specification strings, then exit.
-dumpversion
Print version number, then exit.
-foption
Set the specified compiler option. Many of these control debugging, optimization of code, and special language options. Use the --help -v options for a full listing.
-g
Include debugging information for use with gdb.
-glevel
Provide level amount of debugging information. level must be 1, 2, or 3, with 1 providing the least amount of information. The default is 2.
--help
Print most common basic options, then exit. When used with option -v, print options for all of gcc's subprocesses. For options specific to a target, use --target-help.
-moption
Set the specified machine specific option. Use the --target-help option for a full listing.
-ofile
Specify output file as file. Default is a.out.
-p
Provide profile information for use with prof.
-pass-exit-codes
On error, return highest error code as the exit code, instead of 1.
-pedantic
Warn verbosely.
-pedantic-errors
Generate an error in every case in which -pedantic would have produced a warning.
-pg
Provide profile information for use with gprof.
-print-file-name=file
Print the full path to the library specified by filename file, then exit. This is the library gcc would use for linking.
-print-search-dirs
Print installation directory and the default list of directories gcc will search to find programs and libraries, then exit.
-pipe
Transfer information between stages of compiler by pipes instead of temporary files.
-save-temps
Save temporary files in the current directory when compiling.
-std=standard
Specify C standard of input file. Accepted values are:
iso9899:1990, c89
1990 ISO C standard.
iso9899:199409
1994 amendment to the 1990 ISO C standard.
iso9899:1999, c99, iso9899:199x, c9x
1999 revised ISO C standard.
gnu89
1990 C Standard with GNU extensions (the default value).
gnu99, gnu9x
1999 revised ISO C standard with GNU extensions.
-time
Print statistics on the execution of each subprocess.
-v
Verbose mode. Print subprocess commands to standard error as they are executed. Include gcc version number and preprocessor version number. To generate the same output without executing commands, use the option -###.
-w
Suppress warnings.
-xlanguage
Expect input file to be written in language, which may be c, objective-c, c-header, c++, ada, f77, ratfor, assembler, java, cpp-output, c++-cpp-output, objc-cpp-output, f77-cpp-output, assembler-with-cpp, or ada. If none is specified as language, guess the language by filename extension.
-Bpath
Specify the path directory in which the compiler files are located.
-E
Preprocess the source files, but do not compile. Print result to standard output. This option is useful to meaningfully pass some cpp options that would otherwise break gcc, such as -C, -M, or -P.
-Idir
Include dir in list of directories to search for include files. If dir is -, search those directories specified by -I before the -I- only when #include"file" is specified, not #include <file>.
-Ldir
Search dir in addition to standard directories.
-O[level]
Optimize. level should be 1, 2, 3, or 0 (the default is 1). 0 turns off optimization; 3 optimizes the most.
-S
Compile source files into assembler code, but do not assemble.
-Vversion
Attempt to run gcc version version.
-Wa,options
Pass options to the assembler. Multiple options are separated by commas.
-Wl,options
Pass options to the linker. Multiple options are separated by commas.
-Wp,options
Pass options to the preprocessor. Multiple options are separated by commas.
-Xlinkeroptions
Pass options to the linker. A linker option with an argument requires two -Xlinkers, the first specifying the option and the second specifying the argument. Similar to -Wl.
Preprocessor options
gcc will pass the following options to the preprocessor:
-$
Do not allow $ in identifiers.
-dD, -dI, -dM, -dN
Suppress normal output; print preprocessor instructions instead. See cpp for details.
-idirafterdir
Search dir for header files when a header file is not found in any of the included directories.
-imacrosfile
Process macros in file before processing main files.
-includefile
Process file before main file.
-iprefixprefix
When adding directories with -iwithprefix, prepend prefix to the directory's name.
-isystemdir
Search dir for header files after searching directories specified with -I but before searching standard system directories.
-iwithprefixdir
Append dir to the list of directories to be searched when a header file cannot be found in the main include path. If -iprefix has been set, prepend that prefix to the directory's name.
-iwithprefixbeforedir
Insert dir at the beginning of the list of directories to be searched when a header file cannot be found in the main include path. If -iprefix has been set, prepend that prefix to the directory's name.
-nostdinc
Search only specified, not standard, directories for header files.
-nostdinc++
Suppress searching of directories believed to contain C++-specific header files.
-trigraphs
Convert special three-letter sequences, meant to represent missing characters on some terminals, into the single character they represent.
-undef
Suppress definition of all nonstandard macros.
-Aname[=def]
Assert name with value def as if defined by #assert. To turn off standard assertions, use -A-.
-A-name[=def]
Cancel assertion name with value def.
-C
Retain all comments except those found on cpp directive lines. By default, the preprocessor strips C-style comments.
-Dname[=def]
Define name with value def as if by #define. If no =def is given, name is defined with value 1. -D has lower precedence than -U.
-H
Print pathnames of included files, one per line, on standard error.
-M, -MG, -MF, -MD, -MMD, -MQ, -MT
Suppress normal output and print Makefile rules describing file dependencies. Print a rule for make that describes the main source file's dependencies. If -MG is specified, assume that missing header files are actually generated files, and look for them in the source file's directory. Most of these options imply -E. See cpp for further details.
-Uname
Remove definition of symbol name.
Linker options
gcc will pass the following options to the linker:
-llib
Link to lib.
-nostartfiles
Force linker to ignore standard system startup files.
-nostdlib
Suppress linking to standard library files.
-s
Remove all symbol table and relocation information from the executable.
-shared
Create a shareable object.
-shared-libgcc
Link to a shared version of libgcc if available.
-static
Suppress linking to shared libraries.
-static-libgcc
Link to a static version of libgcc if available.
-usymbol
Force the linker to search libraries for a definition of symbol and to link to the libraries found.
Warning options
-pedantic
Warn verbosely.
-pedantic-errors
Produce a fatal error in every case in which -pedantic would have produced a warning.
-w
Don't print warnings.
-W
Warn more verbosely than normal.
-Waggregate-return
Warn if any functions that return structures or unions are defined or called.
Warn when encountering instances in which pointers are cast to types that increase the required alignment of the target from its original definition.
-Wcast-qual
Warn when encountering instances in which pointers are cast to types that lack the type qualifier with which the pointer was originally defined.
-Wchar-subscripts
Warn when encountering arrays with subscripts of type char.
-Wcomment
Warn when encountering the beginning of a nested comment.
-Wconversion
Warn in particular cases of type conversions.
-Werror
Exit at the first error.
-Wformat
Warn about inappropriately formatted printfs and scanfs.
-Wimplicit
Warn when encountering implicit function or parameter declarations.
-Winline
Warn about illegal inline functions.
-Wmain
Warn about malformed main functions.
-Wmissing-braces
Enable more verbose warnings about omitted braces.
-Wmissing-declarations
Warn if a global function is defined without a previous declaration.
-Wmissing-prototypes
Warn when encountering global function definitions without previous prototype declarations.
-Wnested-externs
Warn if an extern declaration is encountered within a function.
-Wno-import
Don't warn about use of #import.
-Wparentheses
Enable more verbose warnings about omitted parentheses.
-Wpointer-arith
Warn when encountering code that attempts to determine the size of a function or void.
-Wredundant-decls
Warn if anything is declared more than once in the same scope.
-Wreturn-type
Warn about violations of sequence point rules defined in the C standard.
-Wreturn-type
Warn about functions defined without return types or with improper return types.
-Wshadow
Warn when a local variable shadows another local variable.
-Wstrict-prototypes
Insist that argument types be specified in function declarations and definitions.
-Wswitch
Warn about switches that skip the index for one of their enumerated types.
-Wtraditional
Warn when encountering code that produces different results in ANSI C and traditional C.
-Wtrigraphs
Warn when encountering trigraphs.
-Wuninitialized
Warn when encountering uninitialized automatic variables.
-Wundef
Warn when encountering a nonmacro identifier in an #if directive.
-Wunknown-pragmas
Warn when encountering a #pragma directive not understood by gcc.
-Wunused
Warn about unused variables, functions, labels, and parameters.
Pragma directives
#pragma interface [header-file]
Used in header files to force object files to provide definition information via references instead of including it locally in each file. C++-specific.
#pragma implementation [header-file]
Used in main input files to force generation of full output from header-file (or, if it is not specified, from the header file with the same basename as the file containing the pragma directive). This information will be globally visible. Normally the specified header file contains a #pragma interface directive.