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))
-g -glevel -gdwarf -gdwarf-version -gbtf -gctf -gctflevel -gno-prune-btf -ggdb -gno-record-gcc-switches -gstrict-dwarf -gas-loc-support -gas-locview-support -gcodeview -gcolumn-info -gdwarf32 -gdwarf64 -gno-statement-frontiers -gno-variable-location-views -gvariable-location-views=incompat5 -ginternal-reset-location-views -ginline-points -gvms -gz[=type] -gsplit-dwarf -gdescribe-dies -fdebug-prefix-map=old=new -fdebug-types-section -fno-eliminate-unused-debug-types -femit-struct-debug-baseonly -femit-struct-debug-reduced -femit-struct-debug-detailed[=spec-list] -fno-eliminate-unused-debug-symbols -femit-class-debug-always -fno-merge-debug-strings -fno-dwarf2-cfi-asm -fvar-tracking -fvar-tracking-assignments -fvar-tracking-uninit --debug
🌐
SPEC
spec.org › cpu2017 › flags › gcc.2018-02-16.html
GNU Compiler Collection Flags
This flag is likely to be important only to one or two of the floating point speed benchmarks. In accordance with the rules for Base, it is set for all of fpspeed in base. See: http://www.spec.org/cpu2017/Docs/runrules.html#BaseFlags. ... Allows links to proceed even if there are multiple definitions of some symbols. This switch may resolve duplicate symbol errors, as noted in the 502.gcc_r benchmark description.
🌐
Reddit
reddit.com › r/c_programming › gcc flags
r/C_Programming on Reddit: GCC flags
November 18, 2023 -

Hi,

I'm a beginner, barely scratching the surface of C at the moment. Question is, what flags do I choose for compilation? There are some "basic" like -Wall, -W, -pedantic, -ansi, -std=. GCC documentation has a ton of different flags.
Should I learn Make or CMake early to avoid retyping flags every time to compile my source files?Any help, advice are greatly appreciated.
Edit: thank you, guys. Lots of useful and interesting information. You're awesome!

Top answer
1 of 5
13
I'd recommend what you have plus Wextra. As for the second question, simply alias a build command. alias gcc_test='gcc -Wall -Wextra -pedantic -std=c99' Obviously, replace gcc_test with whatever you will remember! Then you can use gcc_test -o myprogram myprogram.c
2 of 5
4
For warnings, start with -Wall -Wextra. Everybody has a different set of flags they like on top of these. You don’t need to get everything now, just start with those two. For standard, start with -std=c11 or -std=c17. Or -std=gnu11 or -std=gnu17. The exact standard is not a big deal. Just pick one, because if you don’t pick one, you’ll get some default and you won’t know which one you’re using. Add -g so you can use the debugger. Add -fsanitize=address when you need help finding memory errors. Avoid using -pedantic. It’s not really that helpful. It just kinda gets in the way. Should I learn Make or CMake early to avoid retyping flags every time to compile my source files? My first recommendation is to use a good build system like Meson, or an IDE like Visual Studio, Code::Blocks, or Xcode. You can use CMake instead, but it kinda sucks. You can use Make, but it sucks a lot. Maybe you like these better. There are a lot of reasons why Make sucks, so I don’t recommend it to anyone. Even if you don’t use one of those build systems, you can at least write a shell script to compile everything. Paste your command line into a text file and then chmod +x that text file, so you can run it to build. For example, you could have a text file named build which looks like this: gcc -Wall -Wextra -g -std=c17 main.c lib.c Then you chmod it +x: $ chmod +x build Then you can run it: $ ./build
Top answer
1 of 16
178

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.

🌐
Linux Handbook
linuxhandbook.com › gcc-flags
Important GCC Flags in Linux
December 16, 2022 - This flag can be crucial for those who are dealing with dynamic linking, especially with shared libraries. While creating shared libraries, you should have the position-independent code so that you can load the shared library from any address rather than a static one. For example, here, I have created a shared library named libmain.so from the main.c: gcc -c -Wall -Werror -fPIC main.c gcc -shared -o libmain.so main.o
🌐
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 › gccint › Flags.html
Flags (GNU Compiler Collection (GCC) Internals)
Nonzero in an insn, call_insn, jump_insn, barrier, or set which is part of a function prologue and sets the stack pointer, sets the frame pointer, or saves a register. This flag should also be set on an instruction that sets up a temporary register to use in place of the frame pointer.
🌐
Red Hat
developers.redhat.com › blog › 2018 › 03 › 21 › compiler-and-linker-flags-gcc
Recommended compiler and linker flags for GCC | Red Hat Developer
July 2, 2024 - Recommended flags vary between distribution versions because of toolchain and kernel limitations. The following table lists recommended build flags (as seen by the gcc and g++ compiler drivers), along with a brief description of which version of Red Hat Enterprise Linux and Fedora are applicable.
Find elsewhere
🌐
Medium
medium.com › @promisevector › c-programming-mastering-flags-in-gcc-32809491f340
C Programming: Mastering Flags in GCC | by Chizaram Nwachukwu | Medium
July 2, 2023 - GCC provides a wide range of flags that allow developers to customize the compilation process and optimize their C code.
🌐
Bookmarklet Maker
caiorss.github.io › C-Cpp-Notes › compiler-flags-options.html
CPP/C++ Compiler Flags and Options
# CC=gcc CC=clang++ $ clang++ file1.cpp file2.cpp file3.cpp \ -std=c++14 -o app.bin -O3 -g \ -Wall -Wextra -pendantic \ -lpthread -lblas -lboost_system -lboost_filesystem \ -I./include/path1/with/headers1 -I./include2 -L./path/lib1 -L./pathLib2 ... Warning flags - enable more verbosity which helps to catch bugs earlier.
🌐
GNU
gcc.gnu.org › onlinedocs › gcc › Optimize-Options.html
Optimize Options (Using the GNU Compiler Collection (GCC))
... Enable two instruction combination passes that run relatively late in the compilation process. One of the passes runs before register allocation and the other after register allocation. The main aim of the passes is to substitute definitions into all uses.
🌐
Interrupt
interrupt.memfault.com › blog › best-and-worst-gcc-clang-compiler-flags
The Best and Worst GCC Compiler Flags For Embedded | Interrupt
October 22, 2019 - You can find all the details about optimization flags in the “Optimization Options” section of the GNU GCC docs11. -Os, optimize for size, is generally the optimization flag you will see used for embedded systems. It enables a good balance of flags which optimize for size as well as speed.
🌐
Mithilesh's Blog
esymith.hashnode.dev › a-comprehensive-guide-to-gcc-flags
A Comprehensive Guide to GCC Flags
April 14, 2024 - -fPIC, -fPIE: These flags enable position-independent code generation, which is necessary for creating shared libraries or executables that can be loaded at any memory address. Understanding and leveraging GCC flags effectively can significantly impact the performance, portability, and maintainability of your code.
🌐
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.
🌐
Linux Questions
linuxquestions.org › questions › linux-software-2 › gcc-flags-553790
GCC Flags
I am reading a book called setting up LAMP and there is a section which teaches how to install mysql from source. The source files are to be compiled,
🌐
SPEC
spec.org › cpu2017 › flags › gcc.2021-07-21.html
GNU Compiler Collection Flags for SPEC CPU
This flag is enabled by default at -O3. It is also enabled by -fprofile-use and -fauto-profile.. -fno-loop-unroll-and-jam disables this optimization. ... Enable Link Time Optimization When invoked with source code, it generates GIMPLE (one of GCC's internal representations) and writes it to ...
🌐
RapidTables
rapidtables.com › code › linux › gcc › gcc-o.html
gcc -o / -O option flags (output / optimization)
gcc -o writes the build output to an output file. gcc -O sets the compiler's optimization level.
🌐
GNU
gcc.gnu.org › onlinedocs › gccint › Internal-flags.html
Internal flags (GNU Compiler Collection (GCC) Internals)
The following flags are passed into lto1 and are not meant to be used directly from the command line.
🌐
Gjbex
gjbex.github.io › Defensive_programming_and_debugging › CodeValidation › Compilers › gcc_flags
Flags for gcc/g++ - Defensive programming and debugging
When you cast away a const qualifier from a pointer, you may inadvertently modify a value you really shouldn't. The compiler can check for this if you add the -Wcast-qual flag.
🌐
Ticalc
tigcc.ticalc.org › doc › comopts.html
GCC Command-Line Options
'-O' turns on the following optimization flags: -fdefer-pop -fmerge-constants -fthread-jumps -floop-optimize -fcrossjumping -fif-conversion -fif-conversion2 -fdelayed-branch -fguess-branch-probability -fcprop-registers '-O' also turns on '-fomit-frame-pointer' on machines where doing so does not interfere with debugging. ... Optimize even more. GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff.