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.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master GCC, G++ and GDB: From Installation to Advanced Debugging

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 --version

If 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                # GDB

CentOS/Red Hat

sudo yum groupinstall "Development Tools" -y   # gcc, g++
sudo yum install gdb -y

Basic compilation

Single‑step compile and run

gcc main.c -o main
main.c

– source file -o main – name of the executable (default a.out)

./main

Separate compilation and linking

gcc -c main.c -o main.o   # compile only, produce object file
gcc main.o -o main       # link object file

This 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
-g

embeds 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 hello

Use -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_program

Simple 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_program

Only modified files need recompilation, which speeds up large builds.

Detailed compilation stages

Preprocess only

gcc -E main.c -o main.i

Generates main.i, the source after macro expansion and header inclusion.

Compile to assembly

gcc -S main.i -o main.s

Produces main.s, the assembly representation.

Assemble to object

gcc -c main.s -o main.o

Creates binary object file main.o (not directly executable).

Link to executable

gcc main.o -o main

The 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 Hat

Segmentation 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 main

Missing debugging symbols

If GDB reports No debugging symbols found, recompile with -g:

gcc -g main.c -o main

Conclusion

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CompilationLinuxC++g++
Liangxu Linux
Written by

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.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.