Flags

What are Flags?

flags are command-line options used to control the behavior of the compiler. These flags can instruct the output, what kind of warnings and errors to display, and various other settings that affect the compilation process. Flags can be very specific and granular, providing a high degree of control over how the compiler generates executable code.

Here are more details about what these flags do and some examples:

Types of GCC Flags

1 General Options:

  • -c: Compile the source files to object files without linking.
gcc -c sourcefile.c
  • -o <file>: Specify the name of the output file.
gcc -o outputfile sourcefile.c
  • -pipe: Use pipes rather than temporary files for communication between the various stages of compilation.
  • -v: Verbose mode; show the commands executed to run the stages of compilation.
gcc -v sourcefile.c
  • --help: Display a list of available options and their purposes.
gcc --help
  • --target-help: Display target-specific command-line options.
  • --version: Display the version of GCC being used.
gcc --version

2 Language Options

  • -std=<standard>: Specify the language standard to which the code should conform (e.g., -std=c99 for C99, -std=c++11 for C++11).
    • For C:
      • c89 or iso9899:1990: ISO C90 (same as ANSI C).
      • c99 or iso9899:1999: ISO C99.
      • c11 or iso9899:2011: ISO C11.
      • gnu89: ISO C90 with GNU extensions.
      • gnu99: ISO C99 with GNU extensions.
      • gnu11: ISO C11 with GNU extensions.
    • For C++:
      • c++98: ISO C++98.
      • c++03: ISO C++03 (technically the same as C++98 with some corrections).
      • c++11: ISO C++11.
      • c++14: ISO C++14.
      • c++17: ISO C++17.
      • c++20: ISO C++20.
      • gnu++98: ISO C++98 with GNU extensions.
      • gnu++11: ISO C++11 with GNU extensions.
      • gnu++14: ISO C++14 with GNU extensions.
      • gnu++17: ISO C++17 with GNU extensions.
      • gnu++20: ISO C++20 with GNU extensions.
  • -x <language>: Explicitly specify the language of the following input files (e.g., -x c).

3 Preprocessor Options

  • -I<dir>: Add a directory to the list of directories to be searched for header files.
gcc -I/path/to/includes sourcefile.c
  • -L<dir>: Add a directory to the list of directories to be searched for libraries.
gcc -L/path/to/libs -o outputfile sourcefile.c -lmylib
  • -D<macro>[=<value>]: Define a preprocessor macro.
  • -U<macro>: Undefine a preprocessor macro.
  • -E: Preprocess the input files and output the preprocessed code without compiling it.

4 Optimization Options

  • -O0: No optimization (default).
  • -O1: Basic optimization.
  • -O2: Moderate optimization.
  • -O3: Maximum optimization.
  • -Os: Optimize for size.
  • -Ofast: Aggressive optimizations that might not adhere to strict standards compliance.

5 Debugging Options

  • -g: Generate debug information.
  • -g<level>: Specify the level of debug information (e.g., -g3 for the most detailed information).
  • -ggdb: Generate debug information optimized for GDB.

6 Warnings and Errors

  • -w: Suppress all warnings.
  • -Wall: Enable most common warnings.
  • -Wextra: Enable additional warnings.
  • -Werror: Treat all warnings as errors.
  • -W<warning>: Enable a specific warning (e.g., -Wshadow).
  • -pedantic: Issue all the warnings required by strict ISO C and ISO C++ standards.

7 Linking Options

  • -L<dir>: Add a directory to the list of directories to be searched for libraries during linking.
  • -l<library>: Link with a specific library (e.g., -lm for the math library).
  • -static: Produce a statically linked executable.
  • -shared: Produce a shared library.
  • -rdynamic: Add all symbols to the dynamic symbol table.
  • -nostdlib: Do not use the standard system libraries or startup files when linking.
  • -nodefaultlibs: Do not use the standard system libraries when linking.
  • -nostartfiles: Do not use the standard startup files when linking.

8 Code Generation Options

  • -fPIC: Generate position-independent code for shared libraries.
  • -fPIE: Generate position-independent executables.
  • -march=<architecture>: Specify the target architecture (e.g., -march=x86-64).
  • -mtune=<processor>: Optimize for a specific processor (e.g., -mtune=generic).

9 Language-Specific Options

  • C/C++ Specific:
    • -fno-exceptions: Disable exception handling in C++.
      • g++ -fno-exceptions sourcefile.cpp
    • -fno-rtti: Disable runtime type information in C++.
      • g++ -fno-rtti sourcefile.cpp
    • -std=c++11: Use the C++11 standard.
    • -fno-common: Allocate separate memory for tentative definitions (improves compatibility with ISO C/C++ standards).
      • gcc -fno-common sourcefile.c
    • -fpermissive: Downgrade some diagnostics about nonconformant code from erros to warnings.
      • g++ -fpermissive sourcefile.cpp
  • Fortran Specific:
    • -ffree-form: Use free-form layout for Fortran source files.
    • -fimplicit-none: Treat all variables as implicitly declared.

10 Profiling Options

  • -pg: Generate profiling information for gprof.
  • -fprofile-generate: Generate data for profile-guided optimization.
  • -fprofile-use: Use profile information for optimization.

11 Threading Options

  • -pthread: Enable multithreading using the POSIX threads library.

12 Miscellaneous Options

  • -m32: Generate 32-bit code.
  • -m64: Generate 64-bit code.
  • -mcmodel=<model>: Specify the code model (e.g., -mcmodel=small).

13 Output Format

  • -s: Compile to assembly code.
gcc -S sourcefile.c
  • -E: Preprocess only; do not compile, assemble, or link.
gcc -E sourcefile.c -o outputfile.i
  • -o <file>: Specify the name of the output file.
gcc -o outputfile sourcefile.c