GCC quick reference
CS 141 Clarkson U.

Compiling

GCC (the GNU Compiler Collection) will compile C, C++, and Assembly language code, using the gcc command. gcc filename recognizes the extensions .c, .h, .cpp, and .S or .s (assembly), among others.

Most of the behavior of GCC is controlled by command line options. This reference provides the most important for this class. Most of this information is taken from the GCC manual, section "GCC Command Options", which has more details about these options and others.

Stopping compilation

Without options, gcc will compile, producing object code, and link the object code, producing an executable.

gcc -c Compile the files, producing object code .o, but do not link.
gcc -S Compile the files to assembly code, not all the way to object code.
-fverbose-asm Add extra comments to the assembly code, making it easier to read.

Other options

gcc -o filename Place output in filename. Without this option, compiling to various stages produces executable a.out (executable), or, if the input file is infile.c, object code infile.o or assembly code infile.s.
gcc -v verbose. Prints more progress messages.
gcc --help Prints command line options available. Combines well with -v.

C options

-funsigned-char, -fsigned-char Compile C code so that the type char is unsigned (or signed).
-fno-builtin Don't use GCCs inline code for small common functions. Call them instead. GCC's inline functions include printf, strcpy, strncpy, and sqrt.

Warnings

-Wall Print warnings about all types of problems and potential problems.
-W Print even more warnings about suspicious code
-w Turn off all warnings

Debugging & Profiling

-g Produce debugging information in a format GDB (the GNU command line debugger) can use, and embed it in the object code or executable.
-ggdb Produce debugging information optimized for GDB use.
-p, -pg Produce profiling (performance) information for prof or gprof. The gprof profiling is more detailed, including information about which functions call which other functions, and how many times.

-fprofile-arcs, -ftest-coverage Counts how many times each branch of an if, if-else, or loop statement is taken. Extremely detailed information about how your program executes, generated and logged when the program is run.
-print-search-dirs Prints the directories where GCC will look for library files, etc.

Optimization

Optimization produces faster and smaller assembly code, object code, and executables. It may produce assembly code that does not correspond line-by-line to the C source code, so that it mixes computation from multiple lines of C source or multiple iterations of a loop.
-O, -O1 Optimize some
-O2 Optimize a lot
-O3 Optimize the most
-funroll-loops Unroll loops where possible
-fno-strict-aliasing Don't assume that a double * and an int * can never point to the same place (for example). Otherwise, -02 might break our floating-point addition emulator code (Assignment 3).
-mcpu=pentium4, -m64, -m32, -msse2 Produce code optimized for Pentium 4, 64-bit x86-64, or processors with the SSE2 extensions, respectively. There are many more options like this. With no options, only code that runs on the Intel 80386 AND all later processors is generated.

Linking

-Ldirectory Searches directory in addition to the standard directories for libraries.
-llibrary Searches for the file liblibrary.a or .so to link with the current object files (if it contains needed functions). Should appear after the input files in your gcc command.
-static Do not link with dynamically loaded libraries (DLLs or .so libraries). Put all necessary functions into the final executable.
-Idirectory Searches directory for header files, in addition to standard include directories.