gcc
Compile C and C++ source code
TLDR
Compile multiple source files into an executable
Activate output of all errors and warnings
Show common warnings, debug symbols in output, and optimize without affecting debugging
Include libraries from a different path
Compile source code into Assembler instructions
Compile source code into an object file without linking
Optimize the compiled program for performance
Display version
SYNOPSIS
gcc [options] files... [-o output_file]
PARAMETERS
-o
Specifies the name of the output executable or object file.
-c
Compiles or assembles the source files but does not link them. Creates object files (.o).
-S
Compiles only; does not assemble or link. Produces assembly code (.s).
-E
Preprocesses only; does not compile, assemble, or link. Outputs preprocessed code to standard output.
-Wall
Enables a large set of common and useful warnings.
-Wextra
Enables additional warnings not included in -Wall.
-g
Produces debugging information for use with debuggers like GDB.
-O
Optimizes the code for speed and size. Levels range from -O0 (no optimization) to -O3, -Os (optimize for size), and -Ofast.
-I
Adds dir to the list of directories to be searched for header files.
-L
Adds dir to the list of directories to be searched for libraries.
-l
Links with the specified library lib. For example, -lm links with libm.a.
-std=
Specifies the C or C++ standard to use (e.g., -std=c99, -std=c++11).
-v
Prints the commands executed by the compiler driver and the compiler's version.
DESCRIPTION
gcc (GNU Compiler Collection) is a highly optimized and widely used compiler system developed by the GNU Project. It serves as the standard compiler for most Unix-like operating systems, including Linux. gcc is not just a C compiler; it's a collection supporting multiple programming languages such as C, C++, Objective-C, Fortran, Ada, Go, and D. Its primary function is to translate human-readable source code into machine-executable binaries. This involves several stages: preprocessing, compilation, assembly, and linking. gcc is renowned for its portability, supporting a vast array of processor architectures and operating systems, making it a cornerstone of open-source software development and a critical tool for developers building applications, libraries, and operating systems.
CAVEATS
While powerful, gcc can have a steep learning curve for complex projects, especially concerning intricate build systems and cross-compilation. Different versions of gcc may have varying default behaviors or support for language features and optimizations, which can lead to portability issues if not managed carefully. Compiler flags can significantly impact performance, binary size, and debugging capabilities; selecting appropriate flags requires understanding.
Large projects can result in long compilation times, necessitating tools like make or ninja for efficient incremental builds.
COMPILATION STAGES
Understanding the stages gcc performs is crucial for effective use and debugging:
1. Preprocessing: Handles directives like #include and #define, expanding macros and including header files. Output is .i (for C) or .ii (for C++) files.
2. Compilation: Translates preprocessed source code into assembly code specific to the target architecture. Output is .s files.
3. Assembly: Converts the assembly code into machine code (binary instructions) in object files. Output is .o files.
4. Linking: Combines the object files with necessary libraries and runtime components to produce a single executable program or a shared library.
HISTORY
gcc was originally written by Richard Stallman in 1987 for the GNU Project as the GNU C Compiler. Its initial purpose was to be a free software compiler for the GNU operating system. It quickly expanded its support to C++, and by 1999, it officially became the GNU Compiler Collection, reflecting its broader language support. gcc has been instrumental in the growth of the free software movement and the development of Linux and other open-source operating systems, evolving continuously with new language standards, optimizations, and target architectures.