GCC Compiler - Command Line Compilation Guide

Learn essential GCC compiler commands for C/C++ development. This guide covers basic compilation, output customization, debugging, and platform-specific builds.

GCC Compiler Guide

The GNU Compiler Collection (GCC) is a powerful and widely used compiler for C, C++, and other programming languages. Mastering its command-line interface is crucial for efficient software development. This guide provides essential GCC commands for various compilation tasks.

Basic Compilation

The most fundamental use of GCC is to compile a source file into an executable. By default, GCC creates an executable file named a.out.

# To compile a file:
gcc file.c

Custom Output File

You can specify a custom name for your output executable using the -o flag.

# To compile a file with a custom output:
gcc -o myprogram file.c

Debugging Symbols

Including debugging symbols is essential for using debuggers like GDB. The -g flag generates debugging information.

# Include debug symbols:
gcc -g myprogram.c

# Include debug symbols with enhanced debugging information for GDB:
gcc -ggdb3 myprogram.c

Target Architecture

GCC allows you to specify the target architecture. For example, to build a 64-bit executable:

# To build for 64 bits:
gcc -m64 myprogram.c

Include Paths

When your code includes header files from custom or system directories, you need to tell GCC where to find them. The -isystem flag adds a directory to the search path for header files and suppresses warnings for files found there.

# Include the directory {/usr/include/myPersonnal/lib/} to the list of path for #include <....>
# With this option, no warning / error will be reported for the files in {/usr/include/myPersonnal/lib/}
gcc -isystem /usr/include/myPersonnal/lib/ myprogram.c

GUI Applications (MinGW)

When compiling GUI applications for Windows using MinGW, you might want to disable the console window that typically appears. The -mwindows flag achieves this.

# To build a GUI for windows (Mingw) (Will disable the term/console):
gcc -mwindows mygui_app.c -o mygui_app.exe

Further Resources