🐧 Linux gcc Command
GNU Compiler Collection (GCC) compiles, assembles, and links C and C++ programs into native executables.
Syntax
$ gcc [options] [source_files] -o output_program
Common Command Options
| Option | Description |
|---|---|
-o | Place the output executable into |
-Wall | Enable all compiler warning messages about questionable constructions |
-Wextra | Enable extra warning flags not included in -Wall |
-O2 | Turn on moderate level 2 code optimization for faster runtime execution |
-O3 | Turn on aggressive optimization (vectorization, inlining) |
-g | Produce debugging information in native format for GDB |
-c | Compile and assemble source files, but do not link (creates .o object file) |
-std= | Specify language standard (e.g. -std=c11, -std=c99, -std=c++17) |
-I | Add directory |
-L | Add directory |
-l | Link with library |
Practical Examples
Simple C Compilation
gcc hello.c && ./a.out
Compiles hello.c into a.out and runs the resulting binary.
Compile with Custom Output Name & Warnings
gcc -Wall -o hello hello.c
Enables all warnings and generates the binary named hello.
Compile Optimized Code with Debugging
gcc -O2 -g -Wall -o myprog main.c
Optimizes performance with -O2 while keeping symbols for GDB.
Link Math Library
gcc -o calc calc.c -lm
Links with the standard C math library (libm.so).
Compile C++ with Modern Standard
g++ -std=c++17 -Wall -o server server.cpp
Uses GNU C++ compiler with C++17 language standard.