💻 Programming

Compiler

Compiler

A program that translates source code into machine code or another language. GCC, Clang, and rustc are representative examples.

📖 Detailed Explanation

A compiler is a program that reads an entire source code and converts it into machine code or another language. It translates a high-level language written by a programmer (such as C, C++, Rust, or Go) into low-level code that the CPU can execute directly, and in the process it also performs syntax checking, type checking, and even optimization.

The compilation process is divided into several stages. First, lexical analysis (Lexing) splits the source code into tokens, and syntax analysis (Parsing) generates an abstract syntax tree (AST). Semantic analysis checks types and variable bindings, and after intermediate code generation and optimization, the final machine code is output.

Representative compilers include GCC (GNU Compiler Collection), Clang/LLVM, MSVC (Microsoft Visual C++), and rustc (the Rust compiler). Each has a different optimization strategy and error message style, and Clang is especially well known for its user-friendly error messages.

A Just-In-Time (JIT) compiler performs compilation at execution time. Java's JVM and JavaScript's V8 engine are representative examples. Unlike Ahead-Of-Time (AOT) compilation, it can optimize using runtime information, but its initial execution speed may be slow.

💻 Code Example

# GCC로 C 프로그램 컴파일
gcc -o hello hello.c

# 최적화 옵션과 함께 컴파일
gcc -O2 -o hello_optimized hello.c

# 디버그 정보 포함
gcc -g -o hello_debug hello.c

# Clang 사용
clang -o hello hello.c

# Rust 컴파일
rustc main.rs

# Go 컴파일 (빌드)
go build -o myapp main.go

# 컴파일 단계별 확인 (GCC)
gcc -E hello.c > hello.i    # 전처리
gcc -S hello.i              # 어셈블리 생성
gcc -c hello.s              # 오브젝트 파일 생성
gcc -o hello hello.o        # 링킹

🗣️ Examples of Practical Conversations

Senior: The release build takes too long. Shall we adjust the optimization level?

Junior: Isn't -O3 the fastest?

Senior: Execution is fast, but compile time is long. During development, -O0 or -O1 is enough, and -O2 for releases only. -O3 also makes the binary larger.

Interviewer: Please explain the difference between a compiler and an interpreter.

Candidate: A compiler converts the entire source code into machine code at once, while an interpreter executes it line by line. Compiled programs run fast but require platform-specific builds, and interpreted languages are convenient for development but their execution speed is relatively slow.

Interviewer: What advantages does a JIT compiler have?

Candidate: It can use runtime information to intensively optimize hot paths. It can inline frequently called functions or specialize them for the types actually used.

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

Reviewer: A few compile warnings are showing up. What about adding -Werror?

Author: There are quite a lot of warnings in the existing code...

Reviewer: Then let's first check all the warnings with -Wall -Wextra and fix them one by one. Ignoring warnings often turns into bugs.

⚠️ Cautions

🔗 Related Terms

📚 Learn More