[2026] C++ Compilation Process Explained: Preprocess, Compile, Assemble, Link

[2026] C++ Compilation Process Explained: Preprocess, Compile, Assemble, Link

이 글의 핵심

From C++ source to executable: preprocessing, compilation, assembly, and linking. Where name mangling happens, how symbols resolve, and how Makefiles and include paths fit in.

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

아래 코드는 cpp를 사용한 구현 예제입니다. 필요한 모듈을 import하고. 코드를 직접 실행해보면서 동작을 확인해보세요.

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

아래 코드는 bash를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.

# 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

아래 코드는 bash를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.

# 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

아래 코드는 bash를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.

# 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

아래 코드는 bash를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.

# 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

다음은 cpp를 활용한 상세한 구현 코드입니다. 필요한 모듈을 import하고. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

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

아래 코드는 cpp를 사용한 구현 예제입니다. 필요한 모듈을 import하고, 에러 처리를 통해 안정성을 확보합니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

// ❌ No header
int main() {
    std::cout << "Hello";  // error
}
// ✅ Include header
#include <iostream>
int main() {
    std::cout << "Hello";
}
# undefined reference
# → missing implementation or library not linked
g++ main.o -lmissing_lib -o myapp

Issue 3: Multiple definitions

아래 코드는 cpp를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.

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

아래 코드는 cpp를 사용한 구현 예제입니다. 필요한 모듈을 import하고, 클래스를 정의하여 데이터와 기능을 캡슐화하며. 코드를 직접 실행해보면서 동작을 확인해보세요.

// a.h
#include "b.h"
// b.h
#include "a.h"  // cycle
// ✅ Forward declaration
class B;  // forward declaration

Compiler options

다음은 bash를 활용한 상세한 구현 코드입니다. 에러 처리를 통해 안정성을 확보합니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

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

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:


Posts that connect well with this topic:

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.

C++, compilation, linking, preprocessing, build — searches like these should surface this article.

... 996 lines not shown ... Token usage: 63706/1000000; 936294 remaining Start-Sleep -Seconds 3