GCC complier reference card

GNU Compiler Collection and toolchains

gcc
gcc-ssa
C compiler driver.

gcc-ssa is the static single assignment branch of gcc.

gcc itself can recognize different file extensions such as .c, .c++, etc, and compile them as such. The only difference between gcc and, say, g++, is gcc does not link the objects to the proper libraries other than GNU libc, so if you have a C++ program, say foo.c++, which uses the Standard C++ library (e.g. std::cout), then

    gcc foo.c++
    
will fail while
    gcc -c foo.c++
    
will work fine.

What g++ does which gcc does not is it will internally append -lstdc++ command-line options if it sees there is a need for linking. Therefore,

    g++ foo.c++
    
is the same as
    gcc foo.c++ -lstdc++
    
To wit, run above commands again with an additional -v switch, and one can see from the output of collect2 about what libraries are linked to.

gcc, g++, gfortran, etc share a common codebase, so any command-line argument that is recognized by gcc should mostly be recognized by g++, gfortran, etc, as well. There are language-specific command-line options. For example, g++ recognizes -static-libstdc++. These language-specific details can be found in source file, say g++spec.c, for example, in GCC codebase.

g++
g++-ssa
C++ compiler driver.
g77
g77-ssa
f77
gfortran
Fortran compiler driver.

GCC 4.x uses gfortran

cc1
cc1plus
f951
The actual C/C++/Fortran compilers.
cppC preprocessor
ldLinker
gdbDebugger
collect2 A ld-like utility used by gcc to do the final linking. It links the user's code against start time initialization/constructor and destructor routines (e.g. crt1.o, crti.o, crtbegin.o, crtend.o, crtn.o)
gprofProfiler. The program must be compiled with -pg option.
asAssembler
arLibrary archiver. It's used for creating, modifying and extracting from archives
ranlibCreate and index the libraries created by ar
c++filtC++ name demangler. It takes a mangled symbol name and displays its original form.
libtoolA generic library support script used in makefiles to simplify the use of shared libraries.
gcovCode coverage analyzer.
nmList the symbols in an object file.
readelfDisplay information of an ELF formatted object file.
objcopyCopy/manipulate an object file. It also translate object files from one format to another.
objdumpDisplay information in an object file.
sizeList names and sizes of sections in an object file.
lddList the dynamic dependencies (shared libraries) of an executable.
ldconfigConfigure dynamic linker run time bindings.

File extensions

.cC source files.
.hC/C++ header files.
.iPreprocessed C source files.
.C/cc/cxx/c++/cp/cpp/CPPC++ source files.
.H/hh/hxx/h++/hp/hpp/HPP/tccC++ header files.
.sAssembler code.
.dDependency files. They contain rules suitable for Makefile describing the dependencies of the source file.

Created by -MD option.

.gcdaFiles created by --coverage option.
gmon.outFiles created by -pg option.

Now the compiler...

Enviromental variables

LANG
LC_XXXX
Localization
TMPDIR Directory for temporary/intermediate files
GCC_EXEC_PREFIX Directory for subprograms (e.g. cc1, collect2..) used by gcc.

The default value is prefix/lib/gcc/ where prefix is the prefix to the installed compiler.

COMPILER_PATH Colon-separated list of directories like PATH.

gcc tries the directories thus specified when searching for subprograms, if it can't find the subprograms using GCC_EXEC_PREFIX.

LIBRARY_PATH Colon-separated list of directories like PATH.

gcc tries the directories thus specified when searching for libraries used in -l (-L has higher precedence though)

CPATH
C_INCLUDE_PATH
CPLUS_INCLUDE_PATH
Colon-separated list of directories like PATH.

cpp tries the directories thus specified when searching for header files (-I has higher precedence though)

C_INCLUDE_PATH is for C specific header files, while CPLUS_INCLUDE_PATH is for C++ specific header files.

DEPENDENCIES_OUTPUT Used by cpp; it shoudl specify how to output dependencies.
LD_RUN_PATH Used by ld to set the RPATH field.

The same as -Wl,-rpath=dir or -R dir command-line options.

Compile

-cCompile *.c and assemble *.s. NO linking.
-IdirAlso search dir for header files.

This can also be controlled by environmental variables C_INCLUDE_PATH and CPLUS_INCLUDE_PATH.

-SCompile *.c into assembly code *.s. NO linking.
-S -masm=intelCompile *.c into Intel syntax assembly code *.s. NO linking.
-fverbose-asm Put extra commentary information in the generated assembly code to make it more readable. For example, memory references to local variables will be annotated with their variable names.
-ERun preprocessor only. The output is sent to stdout.
-C
-CC
When running preprocessor, don't discard comments in the program.

-CC will retain comments contained in macros.

-dMWhen used with -E option, display definitions of all built-in macros, e.g.
gcc -E -dM - < /dev/null
-o filePlace output in file
-vWhen compiling, also display the programs invoked by the compiler.
-###Like -v, but do NOT invoke the programs.
-Q
-ftime-report
-fmem-report
When compiling, display what functions it's compiling and time/memory used in preprocessing/parsing etc.
--version
-dump-version
Print the version number.
-dumpspecsPrint the spec file (which control the default behavior of the "gcc" front-end. It contains rules very much like those found in a typical Makefile)

The spec file usually is at /usr/lib/gcc/<arch>/<ver>/specs, e.g. /usr/lib/gcc/x86_64-redhat-linux/3.4.6/specs

Note: Newer GCC has integrated the spec file into the gcc executable binary itself.

-dumpmachinePrint the machine info.
-wrapper gdb,--argsInvoke all subprograms using the wrapper gdb. For example, cc1 will be invoked as if
gdb -args cc1 ...
@fileRead command-line options from file. The options read are inserted in place of the original @file option.
-BprefixSearch prefix for gcc executables (cc1, cc1plus, collect2..) and necessary libraries. For example:
    -B/usr/libexec/gcc/x86_64-redhat-linux/3.4.6/
    -B/usr/lib/gcc/x86_64-linux-gnu/3.4.6/
    
The equivalent environmental variable is GCC_EXEC_PREFIX.
--print-prog-name=progPrint the full path of gcc executable prog, which can be cc1, cc1plus, collect2...
--print-search-dirsPrint the search directories of gcc executables and libraries
-pipeUse pipes instead of temporary files during compilation. This could speed up the compilation.

On a side note, one can use environmental variable TMPDIR to control where the temporary files go.

C/C++ dialect

-ansi
-std=c90
-std=c++98
Strictly ISO C90 standard. In particular, C programs can't use C++ style "//" comments and inline keyword.

__STRICT_ANSI__ will be defined if this option is used.

-std=sDetermine the language standard. s can be c90, c++98, c++0x ...

__STRICT_ANSI__ will be defined if this option specifies strict conformance.

-pedantic
-pedantic-errors
Label all usage of forbidden extensions as warning/errors.
Should be used with "-std" switch.
-fopenmpEnable OpenMP. (gcc 4.2 and later)

This switch assumes -pthread

-pthreadEnable pthread support.

This flag will also define the macro _REENTRANT, and this is the only difference between -pthread and -lpthread options.

-fno-asmDon't recognize asm, inline or typeof as a keyword, so that these words can be used in C programs as identifiers.
-fno-builtinDon't recognize built-in functions that do not begin with __builtin_ as prefix.

For example, alloca *could* be implemented by gcc not as a function call but just a single instruction. If this occurs, one can't set a breakpoint on alloca calls.

-funsigned-char
-fsigned-char
Whether by default char is signed or unsigned.

Preprocessor

-Dname
-Dname=value
Predefine the macro name, with value 1, or with the specified value
-UnameUn-define the (built-in or -D defined) macro name
-M
-MM
Output a rule (to stdout) suitable for Makefile describing the dependencies of the source file.

-MM only outputs header files not in the system header directories.

This option implies -E option.

-MF fileThe output of -M is written to file. This can also be controlled by environmental variable DEPENDENCIES_OUTPUT.
-MDThe same as -M -MF combined, but doesn't imply -E.

*.d files will be generated.

-Wp,optPass opt to the C preprocessor.

Warning messages

-WallEnable all warnings.
-W
-Wextra
Enable extra warnings.
-wSuppress all warnings.
-WerrorTreat warnings as errors.
-Werror=Treat specific warnings as errors.
-Wfatal-errorsStop after first error.
-Weffc++Warn about violations mentioned in Scott Meyer's Effective C++ book.
-WinlineWarn if a function can't be inlined by compiler but is declared as such in the program.
-fmessage-length=nLine wrapping for the error messages. If n is 0, no line wrapping.

Default n is 0 for gcc and 72 for g++.

Link

-LdirAlso search dir for library files. This can also be controlled by environmental variable LIBRARY_PATH.
-llibraryLink to liblibrary.a

The linker searches libraries and object files in the order they are specified, so

  foo.o -lz bar.o
  
will search library z after file foo.o but before bar.o, so if bar.o refers to functions in z, then -lz must appear AFTER bar.o
-sRemove all symbol information from the executable

-staticProduce statically linked executable

-shared
-fPIC
-Wl,-soname=libfoo.so
Produce shared (dynamic link) libraries. For details, see here.
-rdynamic
-Wl,-E
Export all symbols to the dynamic symbol table (.dynsym section) in the resulting executable binary.

This option is needed for some uses of dlopen or to allow obtaining backtraces from within a program.

To see the content of .dynsym section, use readelf -s a.out command.

-Wl,--version-script=scriptFile When creating shared libraries, specify the version script to be scriptFile.

Symbols in shared libraries can have versions associated with them. The idea of symbol versioning was introduced first in Solaris. The entire shared library which contains these symbols can also have multiple versions associated with it. For example, the following is an excerpt of Glibc's version script:

libc {
  GLIBC_2.0
  GLIBC_2.1
  GLIBC_2.1.1
  ...
  GLIBC_PRIVATE
}
libm {
  GLIBC_2.0
  ...
  GLIBC_2.4
}
and also symbol-specific versions in separate version scripts:
libc {
  GLIBC_2.0 {
    stpcpy; stpncpy; strcasecmp; strcat; strchr; strcmp;
    ....
    strsignal; strspn; strstr; strtok; strtok_r;
  }
  GLIBC_2.1 {
    mempcpy;
    ..
  }
  ...
}

When ld links a binary executable to shared libraries, it will attach all symbols used from shared libraries with a version string. To see them, use

objdump -T a.out
During run-time, ld.so will check if the version strings of undefnied symbols can be found in shared libraries, if not, it will generate an error. See here for details.
-nostartfilesDon't link to the standard startup files (so the start point of a program is not main, but _start).

To compile crt1.o, one has to use this option.

Also see here for examples.

-nodefaultlibsDon't link to the standard system libraries (e.g. libgcc.a).
-nostdlibDon't link to the standard system libraries (e.g. libgcc.a) or startup files.
-static-libgcc
-shared-libgcc
Whether libgcc should be statically or dynamically linked.
-static-libstdc++Link to libstdc++ statically.

This is unlike -static option which does a full static link.

-static-libgfortran[gfortran] Link to libgfortran statically.

This is unlike -static option which does a full static link.

-Wl,optPass opt to the linker.

For example, to link to a library statically, say libstdc++, but link to others dynamically, one can do

    -Wl,-Bstatic -lstdc++ -Wl,-Bdynamic -lm
    
-Wl,-M Enable linker to display link map information.
-Wl,-t Enable linker to display the files it is processing.
-Wl,--gc-sections Tell linker to remove unused sections (garbage collection) when linking objects.

This option should be used together with -ffunction-sections -fdata-sections compiler options.

-Wl,-rpath=dir
-R dir
Tell linker to add dir (a colon-separated list of directories) to the runtime shared/dynamic libraries search path.

The dir takes precedence over the runtime environmental variable LD_LIBRARY_PATH, and there is no way to override it (except using the runtime environmental variable LD_PRELOAD or using tools such as patchelf or chrpath), so its use is deprecated.

Alternatively, one can just set the environmental variable LD_RUN_PATH

-Wl,-rpath=dir,--enable-new-dtags Tell linker to add dir to the runtime shared/dynamic libraries search path.

The dir can be overriden by the runtime environmental variable LD_LIBRARY_PATH

One can use chrpath tool to manipulate RPATH and RUNPATH settings.

-Wl,--start-group
-Wl,--end-group
All the options between this pair are passed to the linker.
-Wl,-e mystart Set the program's entry point to mystart function.
-Wl,-init=myinit When creating an ELF dynamic executable binary, set the binary's INIT tag to myinit function. By default, this function is _init.
-Wl,-fini=myfini When creating an ELF dynamic executable binary, set the binary's FINI tag to myfini function. By default, this function is _fini.
-Wl,-znow When creating an ELF dynamic executable binary, set it to not use lazy binding.
-Wl,-zinterpose When creating an ELF dynamic executable binary, make its symbol symbol table interposes before all symbols but the user's program.
-Wl,-I/foo/ld.so When creating an ELF dynamic executable binary, use /foo/ld.so as the runtime linker.
-Wl,--wrap=symbol Use a wrapper function for symbol. Any undefined reference to symbol will be resolved to __wrap_symbol. Any undefined reference to __real_symbol will be resolved to the actual symbol.

For example,

void *__wrap_malloc (size_t c) {

  printf ("malloc called with %zu\n", c);
  return __real_malloc (c);
}
If you link the user program with above file using --wrap malloc, then all calls to malloc will call the function __wrap_malloc instead. The call to __real_malloc in __wrap_malloc will call the real malloc function.
-print-file-name=library
-print-libgcc-file-name
Print the full path of library which would be used during compiling, and stop. For example:
-print-file-name=libm.a

Debugging

-g
-g3
Produce debugging information.

-g3 will generate DWARF 3 format debugging information, which include extras such as macro definitions (so GDB can inspect them).

-ggdb
-ggdb3
Produce as much debugging information as possible for GDB to use.
-fdump-ir-pass Dump the pass of ir, where ir can be either tree or rtl.

If ir is tree, pass can be, for example, gimple, optimized, all, etc.

If ir is rtl, pass can be, for example, expand, vregs, all, etc.

The name of the dump file is the original source code's name with pass-suffix

-fdump-final-insns[=file]Dump the final internal representation (RTL) to file (*.gkd)
-fdump-parse-tree [gfortran] Dump the internal parse tree before starting code generation.
-dPWhen use with -S option, annotate the assembly output with RTL info.
-dAWhen use with -S option, annotate the assembly output with miscellaneous debugging info.
-dDDump all macro definitions at end of preprocessing.
-dHProduce a core dump whenever a fatal error occurs.
-fdump-core[gfortran] Produce a core dump whenever a fatal error occurs.
-fbacktrace[gfortran] Display a call stack trace whenever a fatal error occurs.
-fcompare-debug[=opts]Compile the programs twice, the second time with opts options added, and then compare RTLs from both.
-save-tempsSave all temporary/intermediate files produced during compiling.

Profiling

-pgProduce profiling information for gprof.
-finstrument-functions
-finstrument-functions-
exclude-file-list=file,file,..

-finstrument-functions-
exclude-function-list=sym,sym,..

Allow to provide user's own instrumentation functions.

The user will need to implement the following two functions: __cyg_profile_func_enter and __cyg_profile_func_exit.

One can then use N. Devillard's etrace to generate call trees.

--coverageUse together with gcov for code coverage analysis.
-fdump-final-insns[=file]Dump the final internal representation (RTL) to file (*.gkd)

Optimization

-O0Don't optimize. This is default.
-O
-O1
Optimize.

When any optimization option is used, __OPTIMIZE__ is defined.

-O2Optimize even more
-O3Optimize yet more.

In particular, it will try to inline a function whenever possible.

-OsOptimize for code size.

This enables all -O2 optimizations that don't increase code size.

This will cause __OPTIMIZE_SIZE__ to be defined.

-ffunction-sections
-fdata-sections
-Wl,--gc-sections
Place each function or data item into its own section. When used with linker option -Wl,--gc-sections and static linking, it can reduce the executable size.

For example, GNU Fortran's and GNU C++'s static runtime libraries are compiled with -ffunction-sections -fdata-sections, so building a statically linked binaries with these two languages, one could use -Wl,--gc-sections.

-fomit-frame-pointerDo not save, set up, and restore stack frame pointers for functions that don't need it.
-ffast-mathOptimize floating-point arithmetic aggressively at cost in accuracy or consistency.

This will cause __FAST_MATH__ to be defined.

In particular, on x86_64, the DAZ (Denormals Are Zero) and FTZ/FZ (Flush To Zero) bits in the x87 FPU control word will be set; the program will be linked to crtfastmath.o, which contains the set_fast_math routine.

The effect of DAZ and FTZ is denormal results from floating-point calculations will be set to 0, and denormal values used as input to floating-point instruction will be treated as 0. See here for details.

-funsafe-math-optimizationsEnable unsafe floating-point operation optimizations, e.g. use associative math, use reciprocal instead of division, disregard floating-point exceptions (division by 0, overflow, underflow, etc).

Also search for flag_unsafe_math_optimizations in the source file gcc/builtins.c in the GCC source tree to see some transcendental function optimizations.

-fexcess-precision=stylestyle can be either fast (default) or standard. The latter allows the excess precision to follow the ISO C99 standard.
-ffloat-storeNever store floating-point numbers in registers.

This option is particular relevant for x87 FPU based floating-point arithmetics, since some optimization switches can change program behavior. For example, compile the following code with -O2 -mfpmath=387 switch

volatile double arg = 7.0;

double x = sqrt(arg);
printf("x = %0.20f\n", x);

volatile double diff = sqrt(arg) - x;
printf("diff = %0.20f\n", diff);
Then it will display different diff when the first printf is removed.
-fno-math-errorDo not set errno after math function (e.g. sqrt) calls.
-fno-trapping-mathGenerate code which assumes floating-point exceptions will never happen.
-funroll-loops
-funroll-all-loops
Unroll the loops.

-funroll-all-loops will unroll all loops, even if their number of iterations is uncertain when the loop is entered.

-funsafe-loop-optimizationsEnable unsafe loop optimizations, e.g. assume loop indices never overflow, etc.
-fvariable-expansion-
in-unroller
When unrolling the loops, create multiple copies of some local variables to improve performance.
-OfastThe same as -O3 and -ffast-math combined.
-combineAllows simultaneous optimization of multiple source files.
-fprofile-generate
-fprofile-use
Profile guided optimization (PGO).
-fwhole-programMake all global functions and variables static.
-fltoLink time optimization (LTO).
-ftree-loop-linearPerform linear loop transformations on tree.
-ftree-parallelize-loops=nParalellize loops by splitting the iterations into n threads.

This switch assumes -pthread

-ftree-vectorize
-ftree-vectorizer-verbose=n
Paralellize/Vectorize loops.

This is enabled by -O3.

-ftree-vectorizer-verbose controls amount of diagnostic information about automatic loop parallelization during compilation.

-fprefetch-loop-arraysGenerate codes to prefetch memory to improve performance of loops that access large arrays.
-march=cpu
-march=native
Generate code for specific cpu, or for current host ("native") on which the programs is compiled.
-mtune=cpu
-mtune=native
Tune for specific cpu, e.g. pentium4, prescott, nocona, core2, amdfam10..., or for current host ("native") on which the programs is compiled.

See this list for possible choices of i386 and x86-64 architectures.

See this list for possible choices of POWER/PowerPC architectures.

-m64Generate 64-bit code. On certain platforms (e.g. POWER/PowerPC), this option must be specified, or by default gcc will only generate 32-bit code.
-mfpmath=387
-mfpmath=sse
Generate floating-point arithmetics for either 387 or SSE.
-msoft-floatUse software floating point library instead of the hardware.
-msimdGenerate code for specific SSE/SIMD extensions.

For x86, simd can be one of Streaming SIMD Extensions (SSE) such as mmx, sse, sse2, sse3, ssse3, sse4.1... The macros __SSE3__, __SSSE3__ .. will be defined.

For POWER/PowerPC simd can be altivec or vsx. The macro __ALTIVEC__ will be defined.

For Cell B.E. simd can be spe.

-mveclibabi=libUse the vector library; lib can be either acml or svml.
-ftls-model=model Use TLS access model, which can be global-dynamic, local-dynamic, initial-exec, local-exec. The default for -fPIC is global-dynamic and initial-exec otherwise.
-mcmodel=model Use code model (for x86) which can be small, medium, kernel, large. If medium is specified, use -mlarge-data-threshold=num option to specify that data greater than num are placed in large data section.
-Wl,-z common-page-size=2M Large page (2 MB) support.

Interesting features

-fmudflap
-fmudflapth
Instrument all pointer/array dereferencing operations (like Valgrind)

-fmudflapth is the multi-threaded version.

Must have libmudflap installed.

-fstack-protector
-fstack-protector-all
Enable protection against buffer overflows such as stack smashing attacks.

This will cause __SSP__ or __SSP_ALL__ to be defined.

-fstack-checkEnable protection against going beyond the boundary of the stack.
-frecord-gcc-switchesRecord the command-line (that is used to compile the code) options in the generated objects.

To see them, use

readelf -p .GCC.command.line a.out
-lmcheck This will cause malloc to perform occasional consistency checks, such as writing past the end of a allocated memory block.

Alternatively, one can call mcheck/mprobe functions in the program.

GCC predefined macros

The following macros have special meaning for C preprocessor cpp. See here and here and for a complete list.

One can use

gcc -E -dM - < /dev/null
to see all predefined macros and their values.

For a comprehensive list of pre-defined C/C++ compiler macros across all platforms, see here

In GCC source code, libcpp/init.c defines most of these macros. gcc/c-common.c defines __FUNCTION__ and related macros.

__cplusplus Is defined if C++ compiler is in use.

This is ANSI C standard macro

__ASSEMBLER__ Is defined when preprocessing assembly language.
__FILE__
__BASE_FILE__
Name of the current input file (as a C string constant)

This is ANSI C standard macro.

__LINE__ Current input line number (as an integer constant)

This is ANSI C standard macro

__FUNCTION__
__func__
If inside a function, the current function name (as a C string constant)

This is ANSI C99 standard macro

__PRETTY_FUNCTION__ If inside a function, the complete type signature and name of the current function (as a C string constant).

For C code, this is the same as __FUNCTION__/__func__.

For C++ code, this also contains the class name (i.e. the decorated name) as well as the type signature (argument types, return types)

__DATE__
__TIME__
Date & time on which the preprocessor is run. (as C string constants)

These are ANSI C standard macros.

__TIMESTAMP__ Last modification time of the input file (as a C string constant)
__STDC__
__STDC_VERSION__
Evaluate to 1 to mean the compiler is ISO standard conformant.

__STDC_VERSION__ evaluates to a C string constant of the form of the form yyyymmL.

__STDC__ is an ANSI C standard macro.

__GNUC__
__GNUC_MINOR__
__GNUC_PATCHLEVEL__
Evaluate to integer constants representing the GNU (C/C++/Fortran) compiler version numbers (major/minor/patch level).
__GLIBC__
__GLIBC_MINOR__
Evaluate to integer constants representing the GNU libc (Glibc) version numbers (major/minor).

These two macros are actually defined in <features.h>

__VERSION__ Evaluate to a C string constant representing the GNU (C/C++/Fortran) compiler version, e.g. 4.1.2 20080704 (Red Hat 4.1.2-48).
__GNUG__
__GFORTRAN__
Is defined when GNU C++/Fortran compiler is in use.
__SSE__
__SSE2__
__SSE3__
__SSSE3__
__ALTIVEC__
Defined for processors that supports SSE/SSE2... instructions or when -msimd is used.
__OPTIMIZE__
__OPTIMIZE_SIZE__
Is defined if any optimization is used.

Furthermore, __OPTIMIZE_SIZE__ is defined if the optimization is for size, not speed.

__FAST_MATH__ Is defined if -ffast-math compiler command-line option is in effect.
_OPENMP Is defined if OpenMP is in effect.
__COUNTER__ This macro expands to sequential integral values starting from 0. In conjunction with the ## operator, this provides a convenient means to generate unique identifiers. See here for an example.
__VA_ARGS__ Use in macros which needs variable numbers of arguments
__WORDSIZE If this macro is defined, it is the number of bits of a word (32 or 64). This macro is Linux specific.
_GNU_SOURCE This one is not a built-in macro. If the user program defines this macro, then all GNU features will be available.
_REENTRANT
_THREAD_SAFE
This one is not a built-in macro. If the user program defines this macro, then reentrant versions of several functions get declared.

Attributes

The keyword __attribute__ allows to specify special attributes of variables, structure fields, data types, or functions. For example,
int x __attribute__ ((aligned (16))) = 0;
causes thr compiler to allocate the global variable x on a 16-byte boundary.

For details, see here for attributes applicable to functions, here for attributes applicable to variables, and here for attributes applicable to data types.

aligned (alignment) Specify a minimum alignment for the variable/structure field/function, measured in bytes.
cleanup (cleanup_fun) For a variable, run the cleanup_fun function when the variable goes out of scope.
deprecated
deprecated (msg)
Display a warning msg during compilation if the variable or function is used anywhere in the source file.
error (msg)
warning (msg)
Display an error/warning msg during compilation if the fucntion is called and is not eliminated through dead code elimination.
packed Specify that a variable or structure field should have the smallest possible alignment: 1 byte for a variable, and 1 bit for a field.
vector_size (bytes) Specify the vector size for the variable. This is only useful if the architecture/processor supports SIMD instructions.

See here for example usage.

Alternatively, one can use architecture-specific data types such as __m128, __m128i, __m128d, __m256 (see below).

always_inline
noinline
Always/Never inline the designated function.
pure

constant

Declare the designated function to have no side effects; the return value only depends on input parameters and at most reads but not writes to global variables.

constant is stricter; the designated function cannot access global variables.

The compiler can use this information to perform more aggressive optimizations.

hot Declare the designated function to be a hot spot, so the compiler can perform more aggressive optimizations.
malloc The designated function will call malloc. This hints the compiler that designated function, if returns a pointer, then this pointer is not the alias of any other pointer.

The compiler can use this information to perform more aggressive optimizations.

nonnull (arg-index, ...) This instructs the compiler that certain arguments of the designated function should be non-null pointers.
noreturn The designated function will not return (such as abort or exit)
optimize (string1, ...) The designated function should be optimized as specified in string1. For example
__attribute__((optimize("O1", "no-gcse")))
is the same as -O1 -fno-gcse in the command-line.
constructor
destructor
constructor (priority)
destructor (priority)
Declare the designated function to be called before/after main/exit is invoked. Functions with these attributes are useful for initializing data.

A constructor with a smaller priority number runs before a constructor with a larger priority number. The opposite relationship holds for destructors.

warn_unused_result The compiler will display a warning if the return value of the designated function is not used. Functions with important but oft-ignored return values, such as read, make excellent candidates for this attribute. Such functions cannot return void.
weak

alias ("target")

Specify the declaration of a function as a weak symbol or a weak alias for target. For example
       void __f () { /* Do something. */; }
       void f () __attribute__ ((weak, alias ("__f")));
      
defines f to be a weak alias for __f.
visibility ("hidden") Mark the symbol as hidden. The effect can be seen by running readelf -s on the executable binary.
ifunc ("resolver_func") Use resolver_func to resolve the address of the current routine. This allows dynamic selection of an optimized version of the routine at runtime. The resolver_func does not take any parameter and returns a function pointer. For example:
        void *my_memcpy (void *dst, const void *src, size_t len)
        {
           ...
        }

        static void (*resolve_memcpy (void)) (void)
        {
            return &my_memcpy;
        }

        void *memcpy (void *, const void *, size_t) __attribute__ ((ifunc ("resolve_memcpy")));
        
So at runtime when ld.so tries to resolve memcpy, it will call resolve_memcpy to get the address of the actual routine, i.e. my_memcpy

This feature needs newer ld (GNU Binutils version 2.20.1) and newer Glibc (version 2.11.1).

For more details, see here.

GCC #pragma directives

The #pragma directive provides more information to the compiler. See here and here for details.

#pragma GCC dependency depfile depfile is a name of file (quoted, C string constant) whose date will be checked and a warning will be issued if the file is newer than the file being compiled.
#pragma GCC poison symbol1 symbol2 ... symbol1 symbol2 .. are (unquoted string) identifiers which will be removed during compilation.
#pragma GCC diagnostic kind option Modify the diagnostic during compilation, e.g.

   #pragma GCC diagnostic warning "-Wformat"
   #pragma GCC diagnostic error "-Wformat"
   #pragma GCC diagnostic ignored "-Wformat"

#pragma message string Display string (C string constant) during compilation.
#pragma weak symbol Declare symbol as a weak symbol. Equivalently, one can use
         __asm__(".weak symbol");
        
A better way to achieve this is through the "weak" function attribute.
#pragma weak symbol1=symbol2 Declare symbol1 as a weak alias of symbol2. Equivalently, one can use
         __asm__(".weak symbol1");
         __asm__(".set symbol1,symbol2");
        
A better way to achieve this is through the "weak, alias" function attributes.

GCC's extension to C

GNU C provides several language features not found in ISO standard C. To test for the availability features, check if the macro __GNUC__ is defined or not.

__label__ Declare labels local to a block scope.
Designated initializers For example, one can write code like this:
 int a[6] = { [4] = 29, [2] = 15 };
 struct point { int x, y; };
 struct point p = { .y = 2, .x = 4 };
 struct point ptarray[10] = { [2].y = 3, [2].x = 4, [0].x = 5 };
&&label1 Get the address of label1 (defined in the current function.) The value has type void *. For example:
int value=1;
const void *labels[] = {&&val_0, &&val_1};
goto *labels[value];

val_0:
  printf("The value is 0\n");
  goto end;
val_1:
  printf("The value is 1\n");
  goto end;
end:
See here for more "labels as values" examples.
case ranges Specify a range of consecutive values in a single case label, as below:
int value = 7;

switch(value) {
case 0 ... 5:
  printf("The value is 0 - 5\n");
  break;
case 6 ... 10:
  printf("The value is 6 - 10\n");
  break;
default:
  printf("The value is ?\n");
}
Note that there must be spaces around the ... otherwise it may be parsed wrong when one uses it with integer values.
__builtin_apply_args
__builtin_apply
__builtin_return
These functions allow user to record the arguments a function received, and call another function with the same arguments, without knowing the number or types of the arguments. They can be used to build wrapper functions. For example:
int foo(int n) {
   return n+1;
}
int foo_wrapper(int n) {
   /* do something with the argument n */

   __builtin_return(__builtin_apply((void *)foo, __builtin_apply_args(),128));
}
See here for details.
__builtin_expect Provide branch prediction hint to the compiler. For example:
#define likely(x)    __builtin_expect(!!(x), 1)
#define unlikely(x)  __builtin_expect(!!(x), 0)

if (likely(!sum)) {
  ...
}
__builtin_constant_p(expression) Return 1 if the value of expression is known at compile time.
__builtin_choose_expr(expression, if_true_expression, if_false_expression) Use if_true_expression if the value of expression is known at compile time and evaluates to non-zero, or use if_false_expression otherwise.
__builtin_prefetch Prefetch data in the given memory region. See here for example.
__builtin_cpu_is The x86 and x86_64 backends have some new built-in functions which can be used to determine the type of CPU in use. For example:
if (__builtin_cpu_is("corei7")) do_corei7();
The names recognized by __builtin_cpu_is are: intel, atom, core2, corei7, nehalem, westmere, sandybridge, amd, amdfam10h, barcelona, shanghai, istanbul, amdfam15h, bdver1, bdver2
__builtin_cpu_supports For example:
if (__builtin_cpu_is ("popcnt")) asm ("popcnt %1,%0" : "=r"(count) : "rm"(n) : "cc");
The names recognized by __builtin_cpu_supports are: cmov, mmx, popcnt, sse, sse2, sse3, ssse3, sse4.1, sse4.2, avx, avx2
__sync_fetch_and_XXX
__sync_XXX_and_fetch
__sync_bool_compare_and_swap
__sync_val_compare_and_swap
__sync_lock_test_and_set
__sync_lock_release
Built-in atomic memory access functions.

XXX can be add, sub, or, and, xor, nand.

__sync_synchronize
__asm__ volatile ("" : : : "memory")
Compiler-generated memory barrier
__builtin___clear_cache Flush cache of the given memory region.
__builtin_return_address(lvl) Get the address of the caller at call chain level lvl.
__builtin_frame_address (lvl) Similar to __builtin_return_address, but it returns the address of the stack frame rather than the return address of the function.

For x86, __builtin_frame_address (0) is in general equal to EBP/RBP register's value.

__typeof__
typeof
Get the type of an expression.

typeofis an ISO C99 standard. See here for examples.

_Bool Declare boolean types.

_Bool is an ISO C99 standard. In practice and for compatibility, one should use bool, true, and false in the header file stdbool.h

__int128
unsigned __int128
Declare 128-bit signed and unsigned integers.
__float80
__float128
Declare 80-bit (extended floating mode) and 128-bit (tetra floating mode) floating types. The 128-bit floating-point arithmetics are handled by software emulation (in GCC source tree, see gcc/config/soft-fp/*.c).
_Complex
__complex__
_Complex80
_Complex128
Declare complex numbers. For example:
float _Complex cf;
__Complex__ int ci;
        
will declare cf as a single-precision complex number and c as a complex integer.

To extract the real/imaginary parts, use __real__ and __imag__ keywords.

_Complex is an ISO C99 standard. See here for examples.

_Decimal32
_Decimal64
...
Declare decimal floating-point variables. These are floating point numbers in radix of 10 instead of the usual 2.

The decimal floating-point arithmetics are handled by software emulation (in GCC source tree, see libdecnumber/ directory).

__m128
__m128i
__m128d
__m256

and all x86 specific stuff

x86 Streaming SIMD Extensions (SSE) intrinsic data types. To use them, include header file xmmintrin.h or x86intrin.h (they are under gcc's directory at /usr/lib/gcc/<arch>/<ver>/include)

In aforementioned directory, one can find the following header files for different instruction extensions:

  • abmintrin.h: AMD's advanced bit manipulation instructions
  • ammintrin.h: SSE4a
  • avxintrin.h: AVX
  • cpuid.h: cpuid instruction
  • emmintrin.h: SSE2
  • fma4intrin.h: FMA4
  • immintrin.h: All Intel's instruction extensions
  • lwpintrin.h: AMD's lightweight profiling instruction extension
  • mm3dnow.h: 3DNow!
  • mmintrin.h: MMX
  • nmmintrin.h: SSE4.2 (Nehalem new instructions)
  • pmmintrin.h: SSE3 (Prescott new instructions)
  • popcntintrin.h: popcnt instruction
  • smmintrin.h: SSE4.1
  • tmmintrin.h: SSSE3 (Tejas new instructions)
  • wmmintrin.h: AES & pclmul (Westmere new instructions)
  • xmmintrin.h: SSE
  • xopintrin.h: XOP
Examples on how to use these data types: here or here.

A complete list of GCC built-in functions which make use of x86 SSE instructions is here and AltiVec/VSX instructions here

__thread Specify a variable to be stored in the thread-local storage
register Variables in specified registers. For example,
register int foo asm ("ebx");
will put the variable (global or local) foo in the register ebx. This allows one to write values directly into a register without using inline assembly.
Arrays of variable length.