Master GCC, G++ and GDB: From Installation to Advanced Debugging
This guide walks you through installing GCC/G++, compiling single‑ and multi‑file C/C++ programs, using essential compiler options, performing step‑by‑step builds, and leveraging GDB for effective debugging, while also covering common errors and their solutions.
Introduction
GCC (GNU Compiler Collection) and GDB are the primary tools for compiling, optimizing and debugging C/C++ programs. GCC performs four stages: preprocessing, compilation, assembly and linking. GDB provides an interactive debugger for the generated binaries.
Installation and verification
Check installation
gcc --versionIf version information is shown, GCC is installed. Verify G++ and GDB similarly with g++ --version and gdb --version.
Ubuntu/Debian
sudo apt update
sudo apt install build-essential -y # gcc, g++ and other tools
sudo apt install gdb -y # GDBCentOS/Red Hat
sudo yum groupinstall "Development Tools" -y # gcc, g++
sudo yum install gdb -yBasic compilation
Single‑step compile and run
gcc main.c -o main main.c– source file -o main – name of the executable (default a.out)
./mainSeparate compilation and linking
gcc -c main.c -o main.o # compile only, produce object file
gcc main.o -o main # link object fileThis workflow lets you recompile only changed files.
Common compiler options
Warnings
gcc -Wall -Wextra main.c -o main -Wall– enable common warnings -Wextra – enable additional warnings
Debugging symbols
gcc -g main.c -o main -gembeds DWARF debug information for GDB.
Optimization levels
# No optimization (default)
gcc hello.c -O0 -o hello
# Basic optimization
gcc hello.c -O1 -o hello
# Recommended for production
gcc hello.c -O2 -o hello
# Aggressive optimization
gcc hello.c -O3 -o helloUse -O0 or -O1 during development, -O2 for production builds, and -O3 only when maximum performance is required and you accept possible compatibility issues.
Multi‑file projects
One‑shot compilation
gcc main.c utils.c -o my_programSimple for small projects.
Separate compilation
gcc -c main.c -o main.o
gcc -c utils.c -o utils.o
gcc main.o utils.o -o my_programOnly modified files need recompilation, which speeds up large builds.
Detailed compilation stages
Preprocess only
gcc -E main.c -o main.iGenerates main.i, the source after macro expansion and header inclusion.
Compile to assembly
gcc -S main.i -o main.sProduces main.s, the assembly representation.
Assemble to object
gcc -c main.s -o main.oCreates binary object file main.o (not directly executable).
Link to executable
gcc main.o -o mainThe linker combines main.o with system libraries to produce the final executable main.
Debugging with GDB
gcc -g main.c -o main # compile with debug info
gdb ./main # start GDB break <line>– set a breakpoint at a specific line. run – start program execution. next – step over the current line (do not enter functions). step – step into the next line (enter functions). print <var> – display the value of a variable. list – show source around the current location; list <line> shows a specific region. backtrace – display the call stack. info breakpoints – list all breakpoints. delete <num> – remove a specific breakpoint; delete removes all. continue – resume execution until the next breakpoint or program exit. quit – exit GDB.
Common issues and solutions
Missing header files
Errors such as stdio.h: No such file or directory indicate that the development package is not installed.
sudo apt install build-essential # Ubuntu/Debian
sudo yum groupinstall "Development Tools" # CentOS/Red HatSegmentation fault
Typical causes: uninitialized pointers, out‑of‑bounds array access, double free, or use‑after‑free.
Fixes:
Initialize pointers before use.
Validate array indices.
Ensure each allocated block is freed exactly once.
Compile with -g and use GDB to locate the fault.
Undefined reference (linker error)
Occurs when an object file or required library is missing.
# Compile all source files
gcc main.c func.c -o main
# Link with the math library
gcc main.c -lm -o mainMissing debugging symbols
If GDB reports No debugging symbols found, recompile with -g:
gcc -g main.c -o mainConclusion
GCC/G++ provide a complete compilation pipeline with configurable warning levels, optimization flags and debug information. GDB complements them with a powerful interactive debugging environment. Mastering these tools enables efficient development, reliable builds and effective troubleshooting of C/C++ applications.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
