💻 Programming

Compile

Compile

The process of converting source code into machine code. Performed by a compiler. Contrasted with an interpreter.

📖 Detailed Explanation

Compilation (Compile) is the process of converting source code written by humans in a high-level programming language into machine code or intermediate code that a computer can execute directly. The program that performs this conversion is called a compiler, and languages such as C, C++, Rust, and Go are typical compiled languages.

A compiler is broadly divided into a frontend and a backend. The frontend separates source code into tokens through lexing and generates an abstract syntax tree (AST) through parsing. In this process, it detects syntax errors and type errors and reports them to the developer.

The backend performs the optimization and code generation stages. The optimization stage improves execution performance by removing unnecessary code, unrolling loops, and inlining functions. The code generation stage produces machine code or assembly code suited to the target architecture.

Compilation translates the code only once before execution, so it has the advantage of fast run-time speed. Interpretation, by contrast, interprets code line by line during execution, which allows quick testing during development but runs more slowly. Today, hybrid approaches that combine the strengths of both, such as JIT (Just-In-Time) compilation, are also widely used.

💻 Code Example

// === 컴파일 과정 예제 (C 언어) ===

// 1. 소스 코드 (hello.c)
// 개발자가 작성한 고급 언어 코드
#include <stdio.h>

int main() {
    int x = 5;
    int y = 10;
    int sum = x + y;
    printf("합계: %d\n", sum);
    return 0;
}

// === 컴파일 단계별 과정 ===

// 2. 전처리(Preprocessing): gcc -E hello.c -o hello.i
//    - #include 헤더 파일 삽입
//    - #define 매크로 확장
//    - 조건부 컴파일 처리

// 3. 컴파일(Compilation): gcc -S hello.i -o hello.s
//    - 렉싱: 토큰 분리 (int, x, =, 5, ;, ...)
//    - 파싱: AST(추상 구문 트리) 생성
//    - 의미 분석: 타입 검사, 스코프 분석
//    - 최적화: 상수 폴딩 (5 + 10 = 15로 미리 계산)
//    - 코드 생성: 어셈블리 코드 출력

// 생성된 어셈블리 코드 예시 (x86-64):
// main:
//     push    rbp
//     mov     rbp, rsp
//     mov     DWORD PTR [rbp-4], 15    ; 최적화: 5+10이 15로 계산됨
//     mov     esi, DWORD PTR [rbp-4]
//     lea     rdi, [rip+.LC0]           ; "합계: %d\n" 문자열
//     call    printf
//     mov     eax, 0
//     pop     rbp
//     ret

// 4. 어셈블(Assembly): gcc -c hello.s -o hello.o
//    - 어셈블리를 기계어 오브젝트 파일로 변환

// 5. 링킹(Linking): gcc hello.o -o hello
//    - 여러 오브젝트 파일과 라이브러리 결합
//    - 심볼 해결 (printf 함수 주소 연결)
//    - 최종 실행 파일 생성

// === 한 번에 컴파일하기 ===
// $ gcc hello.c -o hello    // 모든 단계를 한 번에 수행
// $ ./hello                 // 실행 결과: 합계: 15

🗣️ Say It Like This in Practice

💬 In a Meeting
"This module is type-checked at compile time, so runtime errors should drop significantly. However, a full build takes about 5 minutes, so let's optimize the incremental compilation settings."
💬 In an Interview
"A compiler goes through lexing, parsing, semantic analysis, optimization, and code generation. Modern compiler infrastructure such as LLVM separates the frontend from the backend to support a variety of languages and target architectures."
💬 In a code review
"There are 3 compiler warnings showing up. Turn on the -Wall -Werror flags to treat warnings as errors, and merge after resolving all of them. It can prevent future bugs."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

⚠️ Common Mistakes & Cautions

❌
Ignoring compiler warnings

A warning is a sign of a potential bug. If you brush it off with "it runs anyway, so it's fine," it comes back later as a runtime error or a security vulnerability. Enable all warnings with the -Wall -Wextra flags.

❌
Deploying a debug build

Debug mode compiles without optimization and can run more than 10 times slower. Always compile with a Release build (-O2, -O3 flags) when deploying to production.

✅
Incremental compilation and cache usage

In large projects, use a compile cache such as ccache or sccache. Incremental compilation, which recompiles only the changed files, can cut build time by more than 90%.

🔗 Related Terms

📚 Learn More