Here are mine:

  • -Wextra and -Wall: essential.
  • -Wfloat-equal: useful because usually testing floating-point numbers for equality is bad.
  • -Wundef: warn if an uninitialized identifier is evaluated in an #if directive.
  • -Wshadow: warn whenever a local variable shadows another local variable, parameter or global variable or whenever a built-in function is shadowed.
  • -Wpointer-arith: warn if anything depends upon the size of a function or of void.
  • -Wcast-align: warn whenever a pointer is cast such that the required alignment of the target is increased. For example, warn if a char * is cast to an int * on machines where integers can only be accessed at two- or four-byte boundaries.
  • -Wstrict-prototypes: warn if a function is declared or defined without specifying the argument types.
  • -Wstrict-overflow=5: warns about cases where the compiler optimizes based on the assumption that signed overflow does not occur. (The value 5 may be too strict, see the manual page.)
  • -Wwrite-strings: give string constants the type const char[length] so that copying the address of one into a non-const char * pointer will get a warning.
  • -Waggregate-return: warn if any functions that return structures or unions are defined or called.
  • -Wcast-qual: warn whenever a pointer is cast to remove a type qualifier from the target type*.
  • -Wswitch-default: warn whenever a switch statement does not have a default case*.
  • -Wswitch-enum: warn whenever a switch statement has an index of enumerated type and lacks a case for one or more of the named codes of that enumeration*.
  • -Wconversion: warn for implicit conversions that may alter a value*.
  • -Wunreachable-code: warn if the compiler detects that code will never be executed*.

Those marked * sometimes give too many spurious warnings, so I use them on as-needed basis.

Answer from Alok Singhal on Stack Overflow
🌐
GNU
gcc.gnu.org › onlinedocs › gcc › Option-Summary.html
Option Summary (Using the GNU Compiler Collection (GCC))
See Options for Code Generation Conventions. -fcall-saved-reg -fcall-used-reg -ffixed-reg -fexceptions -fnon-call-exceptions -fdelete-dead-exceptions -funwind-tables -fasynchronous-unwind-tables -fno-gnu-unique -finhibit-size-directive -fcommon -fno-ident -fpcc-struct-return -fpic -fPIC -fpie -fPIE -fno-plt -fno-jump-tables -fno-bit-tests -frecord-gcc-switches -freg-struct-return -fshort-enums -fshort-wchar -fverbose-asm -fpack-struct[=n] -fleading-underscore -ftls-model=model -fstack-reuse=reuse_level -ftrampolines -ftrampoline-impl=[stack|heap] -ftrapv -fwrapv -fwrapv-pointer -fvisibility=[default|internal|hidden|protected] -fstrict-volatile-bitfields -fsync-libcalls -fzero-init-padding-bits=value -Qy -Qn
🌐
Linux Man Pages
man7.org › linux › man-pages › man1 › gcc.1.html
gcc(1) - Linux manual page
When you invoke GCC, it normally does preprocessing, compilation, assembly and linking. The "overall options" allow you to stop this process at an intermediate stage. For example, the -c option says not to run the linker. Then the output consists of object files output by the assembler.
🌐
GNU
gcc.gnu.org › onlinedocs › gcc-3.2.3 › gcc › Invoking-GCC.html
GCC Command Options - GCC, the GNU Compiler Collection
Code Gen Options: Specifying conventions for function calls, data layout and register usage. Environment Variables: Env vars that affect GCC.
🌐
GNU
gcc.gnu.org › onlinedocs › gcc-3.2.2 › gcc › Invoking-GCC.html
GCC Command Options
When you invoke GCC, it normally does preprocessing, compilation, assembly and linking. The "overall options" allow you to stop this process at an intermediate stage. For example, the -c option says not to run the linker.
Top answer
1 of 16
179

Here are mine:

  • -Wextra and -Wall: essential.
  • -Wfloat-equal: useful because usually testing floating-point numbers for equality is bad.
  • -Wundef: warn if an uninitialized identifier is evaluated in an #if directive.
  • -Wshadow: warn whenever a local variable shadows another local variable, parameter or global variable or whenever a built-in function is shadowed.
  • -Wpointer-arith: warn if anything depends upon the size of a function or of void.
  • -Wcast-align: warn whenever a pointer is cast such that the required alignment of the target is increased. For example, warn if a char * is cast to an int * on machines where integers can only be accessed at two- or four-byte boundaries.
  • -Wstrict-prototypes: warn if a function is declared or defined without specifying the argument types.
  • -Wstrict-overflow=5: warns about cases where the compiler optimizes based on the assumption that signed overflow does not occur. (The value 5 may be too strict, see the manual page.)
  • -Wwrite-strings: give string constants the type const char[length] so that copying the address of one into a non-const char * pointer will get a warning.
  • -Waggregate-return: warn if any functions that return structures or unions are defined or called.
  • -Wcast-qual: warn whenever a pointer is cast to remove a type qualifier from the target type*.
  • -Wswitch-default: warn whenever a switch statement does not have a default case*.
  • -Wswitch-enum: warn whenever a switch statement has an index of enumerated type and lacks a case for one or more of the named codes of that enumeration*.
  • -Wconversion: warn for implicit conversions that may alter a value*.
  • -Wunreachable-code: warn if the compiler detects that code will never be executed*.

Those marked * sometimes give too many spurious warnings, so I use them on as-needed basis.

2 of 16
77

Several of the -f code generation options are interesting:

  • -fverbose-asm is useful if you're compiling with -S to examine the assembly output - it adds some informative comments.

  • -finstrument-functions adds code to call user-supplied profiling functions at every function entry and exit point.

  • --coverage instruments the branches and calls in the program and creates a coverage notes file, so that when the program is run coverage data is produced that can be formatted by the gcov program to help analysing test coverage.

  • -fsanitize={address,thread,undefined} enables the AddressSanitizer, ThreadSanitizer and UndefinedBehaviorSanitizer code sanitizers, respectively. These instrument the program to check for various sorts of errors at runtime.

Previously this answer also mentioned -ftrapv, however this functionality has been superseded by -fsanitize=signed-integer-overflow which is one of the sanitizers enabled by -fsanitize=undefined.

🌐
USC CS and ECE
bytes.usc.edu › cs104 › wiki › gcc
G++ Cheatsheet
Here are a couple compiler flags that we don’t as often use in this class. They’re still pretty handy to know. You can read about all the options for using g++ here.
Find elsewhere
🌐
MIT Wiki
wikis.mit.edu › confluence › display › ISTMAP › GCC+Compiler+Options
GCC Compiler Options - MIT Application Platform - MIT Wiki Service
May 15, 2008 - The "-Wextra" option should be considered as well. Optimization should be enabled (normal when using autoconf-based configure scripts), as that enables dataflow analysis and can help detect use of uninitialized variables, unreachable code, etc. Very recent versions of gcc (more recent than ...
🌐
SPEC
spec.org › cpu2017 › flags › gcc.html
GNU Compiler Collection Flags
This switch may resolve duplicate symbol errors, as noted in the 502.gcc_r benchmark description. ... Set the requested page size for the program to one of the available sizes for your system - for example 2M, 4M, 1G. ... This option causes all intrinsic procedures (including the GNU-specific extensions) to be accepted, such as the function imag in 521.wrf_r.
🌐
IBM
ibm.com › docs › en › xl-c-and-cpp-linux › 16.1.0
Supported GCC options - IBM Documentation
March 5, 2021 - If your program targets an NVIDIA GPU device by using the -qoffload option, -gsplit-dwarf affects the debug information for host code only.
🌐
Medium
medium.com › @eightlimbed › commonly-used-gcc-options-explained-8304dfccea20
Commonly Used GCC Options Explained | by Lee Gaines | Medium
December 19, 2019 - Werror — This option turns any warning into an error. Usually warnings are just that: they warn you of potential problems, but they still allow GCC to compile your program.
🌐
GNU
gcc.gnu.org › onlinedocs › gcc-11.3.0 › gccint › Options.html
Options (GNU Compiler Collection (GCC) Internals)
Most GCC command-line options are described by special option definition files, the names of which conventionally end in .opt.
🌐
GNU
gcc.gnu.org › onlinedocs › gcc › RISC-V-Options.html
RISC-V Options (Using the GNU Compiler Collection (GCC))
This is the default when GCC is configured for a ‘riscv64-*-*’ or ‘riscv32-*-*’ but not a ‘riscv64be-*-*’ or ‘riscv32be-*-*’ target. ... Generate stack protection code using canary at guard. Supported locations are ‘global’ for a global canary or ‘tls’ for per-thread canary in the TLS block. With the latter choice the options -mstack-protector-guard-reg=reg and -mstack-protector-guard-offset=offset furthermore specify which register to use as base register for reading the canary, and from what offset from that base register.
🌐
GNU
gcc.gnu.org › onlinedocs › gcc-3.1.1 › gcc › Target-Options.html
Using the GNU Compiler Collection (GCC)
For example, if the driver for version 2.0 is installed as ogcc and that for version 2.1 is installed as gcc, then the command gcc will use version 2.1 by default, while ogcc will use 2.0 by default. However, you can choose either version with either command with the -V option.
🌐
GNU
gcc.gnu.org › onlinedocs › gcc-3.2.1 › gcc › Target-Options.html
Target Options - Using the GNU Compiler Collection (GCC)
For example, if the driver for version 2.0 is installed as ogcc and that for version 2.1 is installed as gcc, then the command gcc will use version 2.1 by default, while ogcc will use 2.0 by default. However, you can choose either version with either command with the -V option.
🌐
GNU
gcc.gnu.org › onlinedocs › gcc › Standards.html
Standards (Using the GNU Compiler Collection (GCC))
This standard, in both its forms, is commonly known as C89, or occasionally as C90, from the dates of ratification. To select this standard in GCC, use one of the options -ansi, -std=c90 or -std=iso9899:1990; to obtain all the diagnostics required by the standard, you should also specify -pedantic ...
🌐
GNU
gcc.gnu.org › onlinedocs › gcc › Warning-Options.html
Warning Options (Using the GNU Compiler Collection (GCC))
Limits the maximum number of error ... point GCC bails out rather than attempting to continue processing the source code. If n is 0 (the default), there is no limit on the number of error messages produced. If -Wfatal-errors is also specified, then -Wfatal-errors takes precedence over this option...
🌐
GNU
gcc.gnu.org › onlinedocs › gcc-9.3.0 › gcc › Option-Index.html
Option Index (Using the GNU Compiler Collection (GCC))
GCC’s command line options are indexed here without any initial ‘-’ or ‘--’. Where an option has both positive and negative forms (such as -foption and -fno-option), relevant entries in the manual are indexed under the most appropriate form; it may sometimes be useful to look up both ...
🌐
Florida State University
cs.fsu.edu › ~baker › ada › gnat › html › gcc_3.html
3. GCC Command Options
Print (on the standard output) a description of the command line options understood by gcc. If the `-v' option is also specified then `--help' will also be passed on to the various processes invoked by gcc, so that they can display the command line options they accept.