GCC Warning options (and clang)
The options listed here are for compiling C programs. They are extra options to make the code less bug prone (like static analyzers).
GCC version 10.2
GCC has warning options helping programmers write better code. When I run tests, I have these options (compiling C):
-g3 -std=gnu11 -fPIC -pipe -mrdrnd -rdynamic -pthread -Wall -Wextra
-g3stores all debug symbols including #defines-std=gnu11is C version I use: GNU C11, the_cleanupattribute is convenient and the macros can values...-mrdrndallows using the hardware random number generator-rdynamicgive us function names in stack traces-pthreadis for threads, it is no longer required with glibc 2.34 but I keep it to able to compile my projects on older linux systems-Walland-Wextraenable many useful warning but not all of them
To make my code more explicit and easier to update in the future, I enabled the following extra warning options:
-Wdouble-promotionwarn implicit conversion to double-Wformat-securitywarn not string literals for print functions-Wformat-signednesswarn format string and argument signedness mismatch-Winit-selfwarn variables initialized with themselves-Wshift-overflow=2warn left shifting into the sign bit-Wswitch-defaultwarn when switch is missing a default case, it helps handling all cases-Wstrict-overflow=4warn about compiler optimizations-Wallocawarn alloca usage, this allow finding code using a lot of stack space-Warith-conversionwarn type conversion with constants-Wduplicated-brancheswarn when an if-else has identical branches-Wduplicated-condwarn about duplicated conditions in an if-else-if chain-Wshadowwarn whenever a local variable or type declaration shadows another variable, parameter, type-Wcast-qualwarn when const qualifier(and other qualifiers) is removed in a cast-Wconversionwarn for implicit conversions that may alter a value. After fixing these warnings, the conversions become explicit in the code-Wdate-timewarn when macros TIME, DATE or TIMESTAMP are encountered as they might prevent bit-wise-identical reproducible compilations-Wstrict-prototypeswarn if a function is declared or defined without specifying the argument types (old style)-Wmissing-prototypeswarn if a global function is defined without a previous prototype declaration-Winvalid-pchwarn if a precompiled header cannot be used
Here are the warning options I don't use:
-Wundefbecause there are warnings for the platform defines that are not set (__FreeBSD__) and it is ok.-Wredundant-decls: in my project, there are lots of redundant declarations, so I don't use this option, but I think it is good warning to enable-Wformat-nonliteral: it issues a warning for globalconst char*, I would like to have a warning when the format string is not readonly because it could be a user input.-Wbad-function-cast: I get the warning when convertingint64_tto double and I run my programs most of the time on x64. The x64 CPUs have the instruction cvtsi2sd so the conversion is done in hardware. I mostly dpn't need this warning.-Wjump-misses-init: When a variable is declared and used in the jumped code between the goto and the label, the warning is issued. I think it is unnecessary and I would like to disable the warning when the goto is ok with #pragma GCC diagnostic ignored "-Wjump-misses-init", but the warning for the label is kept
For more detailed information about GCC warning options check out the official GCC documentation.
Clang version 11
Clang has different options compare to GCC, I compile my programs with both GCC and Clang. When compiling with Clang, I use these warning options:
-Wdouble-promotionsame as GCC-Wformat-securitysame as GCC-Wformat-type-confusionwarn format and variable type mismatch-Winit-selfsame as GCC-Wswitch-defaultsame as GCC-Wstrict-overflow=4same as GCC-Wallocasame as GCC-Wshadowsame as GCC-Wcast-qualsame as GCC-Wconversionsame as GCC-Wdate-timesame as GCC-Wstrict-prototypessame as GCC-Wmissing-prototypessame as GCC-Winvalid-pchsame as GCC-Wmissing-variable-declarations-Wunreachable-code-aggressivewarn about dead code-Wextra-semi-stmtwarn about empty expression statements, for example extra ; like this:puts("string");;-Wused-but-marked-unusedwarn used variables marked as unused-Wcast-alignwarn about memory alignment after cast-Wdocumentationwarn about comments
For more detailed information about Clang warning options check out Diagnostic flags in Clang (short descriptions).
Guides
[https://github.com/ossf/wg-best-practices-os-developers/blob/main/docs/Compiler_Hardening_Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.md](Compiler Options Hardening Guide for C and C++)
hashtags: #cprogramming