Skip to content

Last updated:2026-10-12

GCC Compilation Flags Explained

People usually pass some gcc flags when compiling code; this article walks through some of the more important gcc flags.

1.Optimization Flags

Optimization flags are used to improve the performance of the compiled code. They can speed up the compilation process and produce efficient programs.

Some commonly used optimization flags include:

-O1, -O2, -O3, -Os: these flags control the level of optimization applied.

-O1 applies basic optimizations, while -O2 and -O3 provide more advanced optimization. -Os optimizes for code size.

-finline-functions, -funroll-loops: these flags control function inlining and loop unrolling optimizations respectively, aiming to eliminate function-call overhead and reduce loop iterations.

2.Debugging Flags

Debugging flags help provide extra information so that debugging tools can trace and analyze the code.

Some commonly used debugging flags include:

-g: this flag includes debugging symbols in the compiled executable.

-ggdb: this flag makes GCC produce debugging information in a format suitable for the GNU Debugger (GDB).

3.Warning Flags

Warning flags help identify potential problems, coding errors, or problematic practices in the code. By enabling warning flags, the compiler warns about these situations, allowing developers to improve code quality.

Some commonly used warning flags include:

-Wall: this flag enables a comprehensive set of warnings covering a wide range of potential problems.

-Werror: this flag treats warnings as errors, stopping the compilation process whenever a warning is encountered.

4.Preprocessor Flags

Preprocessor flags deal with the directives in source code that start with #. These flags define macros and control compilation.

Some commonly used preprocessor flags include:

-D: this flag is used to define macros during compilation. For example, -DDEBUG can be used to define the DEBUG macro, enabling conditional compilation based on whether it exists.

-I: this flag is used to specify additional directories where header files are located. It helps the preprocessor find the necessary header files when compiling code.

5.Linker Flags

Linker flags control the linking process, which takes place after compilation, combining object files and libraries to produce the final executable.

Some commonly used linker flags include:

-l: this flag is used to link external libraries. For example, -lpthread links the POSIX threads library.

-L: this flag is used to specify additional directories where libraries are located. It helps the linker find the required libraries during linking.

6.Platform-Specific Flags

GCC provides flags targeted at specific platforms, allowing developers to target a particular architecture or enable specific platform features. These flags are very useful when developing code for specific hardware or operating systems.

Platform-specific flags include:

-march: this flag specifies the target architecture. For example, -march=native tells GCC to optimize for the architecture of the host machine.

-m32 and -m64: these flags specify whether to compile for a 32-bit or 64-bit target.

7.Miscellaneous Flags

Besides the categories mentioned above, there are also some other flags.

Some commonly used miscellaneous flags include:

-std: this flag specifies the version of the C language standard to use. For example, -std=c99 selects the C99 standard. -pedantic: this flag enables strict adherence to the selected C language standard, issuing extra warnings for non-standard or questionable code constructs. -Wextra: this flag enables additional warnings beyond those enabled by -Wall, providing a more thorough analysis of the code.

Note that the availability and behavior of flags may vary across GCC versions and platforms. It is therefore recommended to consult the GCC documentation and related resources for detailed information about specific flags and their usage.

Full documentation address:

https://gcc.gnu.org/onlinedocs/gcc/

Comments