Resources:
Summary:
# Options I use
-Wall -Wextra -Werror -pedantic -std=c17 -Wformat=2 -Wformat-truncation -Werror=vla -Wundef -Wfloat-conversion -Wdouble-promotion -Wshadow -Wcast-align
# Warnings I disable
-Wno-unused-variable -Wno-unused-function -Wno-unused-parameter -Wno-missing-field-initializers -Wno-missing-braces
# Special behavior flags I use
-fno-strict-aliasing -fwrapv -fwrapv-pointer
# Debug flags
-O0 -g -DDEBUG
# Optimized flags
-O3 -march=native
# Other
-Icode/ # Add "code/" to include directories
-lm # Link library "m"
-o blah # Sets output file to "blah"
# Also: When using interfacing with Linux,
# you might need to define _POSIX_C_SOURCE
# or _GNU_SOURCE
Options I use:
-Wall: Enable a lot of warnings (but contrary to the name, not all)-Wextra: Enable even more warnings-Werror: Make the compilation fail if any warnings are generated-pedantic: Create a warning if the code is not following the C standard (e.g. using gnu-extensions)-std=c17: Specify the standard to follow (here: C17)-Wformat=2: For e.g. printf / scanf, check the format strings against the provided function parameters
and warn on mismatchprintf-style arguments, you
can enable warnings by adding e.g. __attribute__((format(printf, 1, 2))) in front of the function definitionformat(archetype, string-index, first-to-check)-Wformat-truncation: For e.g. snprintf, warn if the output might be truncated-Wundef: Warn if an undefined identifier is evaluated in an #if directive-Wfloat-conversion: Warn about implicit conversions which reduce the precision of a real value (e.g. double -> float or float -> int)-Wdouble-promotion: Warn about implicit conversion from float to double-Wshadow: Warn if a variable or type shadows another.-Wcast-align: Warn if casting a pointer requires a stricter alignment.
I used to use -Wsign-conversion:
stb_-headers use this warningSpecial behavior flags:
-fno-strict-aliasing: Disable the compiler optimization that two pointers of (almost all) differing types are always referring to different types.
I don't like this behavior (of the C standard).-fwrapv: Assume that signed arithmetic overlfwo wraps around using twos-complement. This is true for all architectures I care about.-fwrapv-pointer: Same as above but for pointers.Warnings I disable:
-Wno-unused-variable: Don't warn on unused variables-Wno-unused-function: Don't warn on unused functions-Wno-unused-parameter: Don't warn on unused function parameters-Wno-missing-field-initializers: When initializing a struct, don't warn if an initializer of some field is missing
(C standard mandates that a field without an initializer is implicitly set to 0)-Wno-missing-braces: For nested struct / unions, don't warn if an initializer is not fully bracketedDebug flags:
-O0: Disable optimizations-g: Produce debugging information-DDEBUG: Predefines the macro DEBUG to 1
(preprocessor can then check for debug/release builds using #ifdef DEBUG [...] #else [...] #endif)Release flags:
-O3: Enable (most) optimizations-march=native: Enable usage of instructions which are supported by the local machine