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))
-all_load -allowable_client -arch name -arch_errors_fatal -asm_macosx_version_min=version -bind_at_load -bundle -bundle_loader -client_name -compatibility_version -current_version -dead_strip -dependency-file -dylib_file -dylinker -dylinker_install_name -dynamic -dynamiclib -exported_symbols_list -fapple-kext -fconstant-cfstrings -ffix-and-continue -filelist -findirect-data -flat_namespace -force_cpusubtype_ALL -force_flat_namespace -framework name -gfull -gused -headerpad_max_install_names -iframework -image_base -init symbol-name -install_name -keep_private_externs -matt-stubs -mconstant-cfs
🌐
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 Man Pages
man7.org › linux › man-pages › man1 › gcc.1.html
gcc(1) - Linux manual page
You may want to use the -fno-gnu-keywords flag instead, which has the same effect. In C99 mode (-std=c99 or -std=gnu99), this switch only affects the "asm" and "typeof" keywords, since "inline" is a standard keyword in ISO C99. -fno-builtin -fno-builtin-function Don't recognize built-in functions that do not begin with __builtin_ as prefix. GCC ...
🌐
SPEC
spec.org › cpu2017 › flags › gcc.2018-02-16.html
GNU Compiler Collection Flags
Do not transform names of entities specified in the Fortran source file by appending underscores to them. ... Let the type "char" be unsigned, like "unsigned char". Note: this particular portability flag is included for 526.blender_r per the recommendation in its documentation - see http://www.spec.org/cpu2017/Docs/benchmarks/526.blender_r.html.
🌐
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
🌐
GNU
gcc.gnu.org › onlinedocs › gccint › Internal-flags.html
Internal flags (GNU Compiler Collection (GCC) Internals)
-fltrans-output-list=file This option specifies a file to which the names of LTRANS output files are written. This option is only meaningful in conjunction with -fwpa.
🌐
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 - When you are using one of these distributions with the included compiler, this environment is recreated, requiring an extensive list of flags to be specified. 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.
🌐
GNU
gcc.gnu.org › onlinedocs › gcc-2.95.2 › gcc_2.html
Using and Porting the GNU Compiler Collection (GCC): Invoking GCC
GCC recognizes files with these names and compiles them as C++ programs even if you call the compiler the same way as for compiling C programs (usually with the name gcc). However, C++ programs often require class libraries as well as a compiler that understands the C++ language--and under ...
Find elsewhere
🌐
GNU
gcc.gnu.org › onlinedocs › gcc › Preprocessor-Options.html
Preprocessor Options (Using the GNU Compiler Collection (GCC))
If not specified, only the precompiled ... Precompiled Headers) together with -E. It inserts a special #pragma, #pragma GCC pch_preprocess "filename" in the output to mark the place where the precompiled header was found, and its filename....
🌐
GNU
gcc.gnu.org › onlinedocs › gccint › Flags.html
Flags (GNU Compiler Collection (GCC) Internals)
In an RTL dump, this flag is represented as ‘/f’. ... In reg expressions, it is 1 if the register has its entire life contained within the test expression of some loop.
🌐
Bookmarklet Maker
caiorss.github.io › C-Cpp-Notes › compiler-flags-options.html
CPP/C++ Compiler Flags and Options
c++ - How to build in release mode with optimizations in GCC? - Stack Overflow · MSVC Native tools: CC = cl.exe · C and C++ Compiler - Can compile both C and C++ code depending on the flag. By default it compiles C++ code. rc.exe => Resource Compiler. LD = link.exe · C++ Linker. AS = ml · Assembler · AR = lib · Archiver · Compiler: cl.exe · /nologo - Suppress microsoft's logo · /out:<file.exe> - Set output file name.
🌐
Earthly
earthly.dev › blog › make-flags
Understanding and Using Makefile Flags - Earthly Blog
July 24, 2023 - CC = gcc CFLAGS = -g -Wall CPPFLAGS = - I /usr/foo/bar # Search for header files in /usr/foo/bar main.o: main.c $(CC) $(CPPFLAGS) $(CFLAGS) -c main.c · You can use LDFLAGS to pass extra flags to the linker lD. Similar to CPPFLAGS, these flags are automatically passed to the linker when the compiler invokes it. The most common use is to specify directories where the libraries can be found, using the -L option. You should not include the names of the libraries in LDFLAGS; instead they go into LDLIBS.
🌐
DEV Community
dev.to › mithil467 › 10-useful-gcc-flags-that-i-use-regularly-4lcb
10 Useful GCC Flags That I Use Regularly - DEV Community
June 14, 2020 - I have been of the idealogy (which is silly) that if something can be done in c++, do it in c++. Yes, I am slowly moving towards other feasible solutions for certain use cases. Here are my frequently used gcc flags. ... The default executable name is a.exe or a.out or something similar depending ...
🌐
Medium
medium.com › @promisevector › c-programming-mastering-flags-in-gcc-32809491f340
C Programming: Mastering Flags in GCC | by Chizaram Nwachukwu | Medium
July 2, 2023 - Therefore, it’s recommended to consult the GCC documentation and relevant resources for detailed information about specific flags and their usage. ... By understanding and utilizing these flags effectively, developers can fine-tune their C code, optimize performance, ensure compatibility, and address specific requirements of their projects. Experimenting with ...
🌐
Ticalc
tigcc.ticalc.org › doc › comopts.html
GCC Command-Line Options
Possible values for language are: c c-header cpp-output c++ c++-cpp-output objective-c objc-cpp-output assembler assembler-with-cpp ada f77 f77-cpp-input ratfor java treelang ... Turn off any specification of a language, so that subsequent files are handled according to their file name suffixes (as they are if '-x' has not been used at all). ... Normally the gcc program will exit with the code of 1 if any phase of the compiler returns a non-success return code.
🌐
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,
🌐
Boston University
bu.edu › tech › support › research › software-and-programming › programming › compilers › gcc-compiler-flags
GNU and LLVM Compiler Flags : TechWeb : Boston University
gcc -O3 -march=sandybridge -mtune=intel -c mycode.cpp · Note that selecting specific SIMD instructions with the -mavx* flag or -march=arch flag will restrict compatibility with compute nodes unless the job is submitted with this qsub flag: -l cpu_arch=compatible_arch. The compatible_arch value is an architecture name that matches the SIMD instructions.
🌐
Gjbex
gjbex.github.io › Defensive_programming_and_debugging › CodeValidation › Compilers › gcc_flags
Flags for gcc/g++ - Defensive programming and debugging
It is always good practice to switch on "all warnings" with -Wall. Contrary to expectations, -Wall doesn't activate all warnings though. A number of extra warnings on top of those enabled by -Wall can be switched on by specifying -Wextra. It is highly recommended to always use these two compiler options, and eliminate all warnings reported by them, i.e., ... The gcc compiler can also check for language specification conformity." The -Wpedantic flag ...