C++ Compilation Process Explained: Preprocess, Compile, Assemble, Link
이 글의 핵심
Walk through the four stages from source to executable: preprocessing, compilation, assembly, and linking. Name mangling occurs during compilation; symbol resolution happens at link time.
The compilation pipeline
From source to executable, C++ builds go through preprocessing → compilation → assembly → linking. Name mangling happens during compilation; symbol resolution happens at link time. Makefiles and include paths automate this workflow.
Source (.cpp) → preprocess → compile → assemble → link → executable
1. Preprocessing
// main.cpp
#include <iostream>
#define PI 3.14
int main() {
std::cout << PI << std::endl;
}
# Inspect preprocessor output
g++ -E main.cpp -o main.i
2. Compilation
# C++ to assembly
g++ -S main.i -o main.s
3. Assembly
# Assembly to machine code
g++ -c main.s -o main.o
4. Linking
# Object file to executable
g++ main.o -o main
Practical examples
Example 1: Step-by-step build
# All-in-one
g++ main.cpp -o main
# Step by step
g++ -E main.cpp -o main.i # preprocess
g++ -S main.i -o main.s # compile
g++ -c main.s -o main.o # assemble
g++ main.o -o main # link
Example 2: Multiple sources
# Compile each file
g++ -c main.cpp -o main.o
g++ -c util.cpp -o util.o
# Link
g++ main.o util.o -o myapp
Example 3: Linking libraries
# Static library
g++ main.o -L./lib -lmylib -o myapp
# Shared library
g++ main.o -L./lib -lmylib -Wl,-rpath,./lib -o myapp
Example 4: Optimization flags
# Debug symbols
g++ -g main.cpp -o main
# Optimization
g++ -O2 main.cpp -o main
# Warnings
g++ -Wall -Wextra main.cpp -o main
Preprocessor directives
// Include
#include <iostream> // system header
#include "myheader.h" // project header
// Macros
#define MAX 100
#define SQUARE(x) ((x) * (x))
// Conditional compilation
#ifdef DEBUG
#define LOG(x) std::cout << x
#else
#define LOG(x)
#endif
// Include guard
#ifndef MYHEADER_H
#define MYHEADER_H
// contents
#endif
Common issues
Issue 1: Missing header
// ❌ No header
int main() {
std::cout << "Hello"; // error
}
// ✅ Include header
#include <iostream>
int main() {
std::cout << "Hello";
}
Issue 2: Link errors
# undefined reference
# → missing implementation or library not linked
g++ main.o -lmissing_lib -o myapp
Issue 3: Multiple definitions
// header.h
int globalVar = 10; // ❌ ODR violation if included in multiple TUs
// ✅ Declaration only (header)
extern int globalVar;
// source.cpp
int globalVar = 10; // single definition
Issue 4: Circular dependencies
// a.h
#include "b.h"
// b.h
#include "a.h" // cycle
// ✅ Forward declaration
class B; // forward declaration
Compiler options
# Language standard
g++ -std=c++17 main.cpp
# Warnings
g++ -Wall -Wextra -Werror main.cpp
# Optimization
g++ -O0 # none
g++ -O1 # basic
g++ -O2 # recommended for release
g++ -O3 # aggressive
# Debug
g++ -g main.cpp
# Preprocessor define
g++ -DDEBUG main.cpp
FAQ
Q1: What are the build stages?
A: Preprocessing → compilation → assembly → linking.
Q2: What is a .o file?
A: An object file. It contains machine code for one translation unit.
Q3: What causes link errors?
A:
- Missing function definitions
- Library not linked
- Undefined symbols
Q4: Which optimization level?
A:
- -O0: debugging
- -O2: typical release builds
- -O3: maximum optimization
Q5: How do I inspect preprocessing?
A: Use g++ -E.
Q6: Further reading?
A:
- Linkers and Loaders
- GCC documentation
- C++ Primer
Related: Linking, Name mangling, Makefile, Include path.
Related posts (internal links)
Posts that connect well with this topic:
- C++ Linking Guide
- C++ Name Mangling
- C++ Makefile Guide
- C++ Include Path
Practical tips
Tips you can apply immediately.
Debugging
- When something breaks, start with compiler warnings.
- Reproduce issues with a minimal test case.
Performance
- Do not optimize without profiling.
- Define measurable metrics first.
Code review
- Pre-check areas that reviews often flag.
- Follow your team’s coding conventions.
Practical checklist
Things to verify when applying these ideas in real projects.
Before coding
- Is this technique the best fit for the problem?
- Can teammates understand and maintain this code?
- Does it meet performance requirements?
While coding
- Are all compiler warnings addressed?
- Are edge cases considered?
- Is error handling appropriate?
During review
- Is intent clear?
- Are tests sufficient?
- Is documentation in place?
Use this checklist to reduce mistakes and improve quality.
Keywords (search)
C++, compilation, linking, preprocessing, build — searches like these should surface this article.
Related posts
- C++ CMake
- C++ Linking
- C++ Macro Programming
- C++ Name Mangling
- C++ One Definition Rule