If you're new, -std=c17 -pipe -g -Wall -Wextra -Wpedantic, and -O3 -march=native if u need optimization. Answer from REmorin on reddit.com
🌐
SPEC
spec.org › cpu2017 › flags › gcc.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.
🌐
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.
Discussions

What flags do you add to your gcc compiler ?
If you're new, -std=c17 -pipe -g -Wall -Wextra -Wpedantic, and -O3 -march=native if u need optimization. More on reddit.com
🌐 r/cprogramming
15
7
September 19, 2022
What are the useful GCC flags for C? - Stack Overflow
Beyond setting -Wall, and setting -std=XXX, what other really useful, but less known compiler flags are there for use in C? I'm particularly interested in any additional warnings, and/or and turning More on stackoverflow.com
🌐 stackoverflow.com
GCC flags
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 More on reddit.com
🌐 r/C_Programming
42
17
November 18, 2023
What does the GCC flag "-Wl" mean?
It's simply used to pass arbitrary options to the linker. It doesn't have any significance to GCC other than that (i.e. it's not a warning option, even though it starts with -W). More on reddit.com
🌐 r/C_Programming
10
7
October 17, 2020
🌐
GNU
gcc.gnu.org › onlinedocs › gcc › Option-Summary.html
Option Summary (Using the GNU Compiler Collection (GCC))
-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
🌐
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.
🌐
Stanford
web.stanford.edu › class › archive › cs › cs107 › cs107.1186 › unixref › topics › gcc
gcc (how to compile c programs)
gcc takes many different command line options (flags) that change its behavior. One of the most common flags is the "optimization level" flag, -O (uppercase 'o'). gcc has the ability to optimize your code for various situations.
Find elsewhere
🌐
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 - NOTE: A pretty slick feature with GCC is being able to enable format checks for custom functions7. This is accomplished using the format attribute: int my_printf (void *a, const char *fmt, ...) __attribute__ ((format (printf, 2, 3))); Another interesting set of formatter warning flags not enabled by default is -Wformat-overflow and -Wformat-truncation.
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.

🌐
GitHub
gist.github.com › g-berthiaume › 74f0485fbba5cc3249eee458c1d0d386
A useful list of gcc compiler options · GitHub
$ gcc myprog.c -o myprog -Wall -Wextra -pedantic -std=c11 [...] NOTES: You can turn individual warnings on/off with -Wthis-warning or -Wno-this-warning. Thanks to reddit users u/5tarrealm5, u/Elronnd, u/maep and u/capilot for inspiring this notes. ... Please note that this gist has not been updated since 6 years ago. Maybe, one day, I'll update it, but for the moment please check the -fsanitize flag...
🌐
GNU
gcc.gnu.org › onlinedocs › gcc › Warning-Options.html
Warning Options (Using the GNU Compiler Collection (GCC))
The warning message for each controllable warning includes the option that controls the warning. That option can then be used with -Werror= and -Wno-error= as described above. (Printing of the option in the warning message can be disabled using the -fno-diagnostics-show-option flag.)
🌐
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
🌐
IBM
ibm.com › docs › en › xl-c-and-cpp-linux › 16.1.0
Supported GCC options
We cannot provide a description for this page right now
🌐
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
🌐
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 › @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.
🌐
Arch Linux Forums
bbs.archlinux.org › viewtopic.php
Get gcc to show all the CFLAGS including those auto set? [SOLVED] / Programming & Scripting / Arch Linux Forums
March 15, 2018 - This doesn't show the options that gcc was configured to enable by default, of course. So -v would do that, at least by virtue of printing the configure line from the gcc PKGBUILD itself... but that would take some parsing. It does also show you the resolved FLAGS for -march=native so that's good.
🌐
Bookmarklet Maker
caiorss.github.io › C-Cpp-Notes › compiler-flags-options.html
CPP/C++ Compiler Flags and Options
June 4, 2021 - # 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-3.2 › gcc › Invoking-GCC.html
Using the GNU Compiler Collection (GCC)
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.
🌐
Gentoo Wiki
wiki.gentoo.org › wiki › GCC_optimization › en
GCC optimization - Gentoo wiki
September 5, 2025 - When using a compiler version prior to GCC 12, in order to take full advantage of AVX YMM registers, the -ftree-vectorize or -O3 options should be used as well[1]. Even though the CHOST variable in /etc/portage/make.conf specifies the general architecture used, -march should still be used so that programs can be optimized for the system specific processor. x86 and x86-64 CPUs (among others) should make use of the -march flag.
🌐
Boston University
bu.edu › tech › support › research › software-and-programming › programming › compilers › gcc-compiler-flags
GNU and LLVM Compiler Flags : TechWeb : Boston University
Programs that involve intensive floating-point calculations inside of loops can additionally be compiled with the -xarch flag. For maximum cross-compatibility across the SCC compute nodes and probable highest performance a combination of flags should be used: gcc -O3 -march=sandybridge -mtune=intel -c mycode.cpp