open-courses
  • 公开课笔记
  • CMU 15-445/645 Database Systems
    • Relational Data Model
    • Advanced SQL
    • Database Storage
    • Buffer Pools
    • Hash Tables
    • Tree Indexes
    • Index Concurrency Control
    • Query Processing
    • Sorting&Aggregations
    • Join Algorithms
    • Query Optimization
    • Parallel Execution
    • Embedded Database Logic
    • Concurrency Control Theory
    • Two Phase Locking
    • Timestamp Ordering Concurrency Control
    • Multi-Version Concurrency Control
    • Logging Schemes
    • Database Recovery
    • Introduction to Distributed Databases
    • Distributed OLTP Databases
    • Distributed OLAP Databases
  • UCB - CS162
    • OS intro
    • Introduction to the Process
    • Processes, Fork, I/O, Files
    • I/O Continued, Sockets, Networking
    • Concurrency: Processes & Threads
    • Cooperating Threads, Synchronization
    • Semaphores, Condition Variables, Readers/Writers
    • Scheduling
    • Resource Contention & Deadlock
    • Address Translation, Caching
    • File System (18,19,20)
    • Distributed Systems, Networking, TCP/IP, RPC (21,22)
    • Distributed Storage, Key-Value Stores, Security (23)
    • Security & Cloud Computing (24)
    • Topic: Ensuring Data Reaches Disk
  • MIT - 6.006
    • Sequence and Set Interface
    • Data Structure for Dynamic Sequence Interface
    • Computation Complexity
    • Algorithms and Computation
    • Structure Of Computation
    • Graph & Search
    • Tree & Search
    • Weighted Shortest Paths
    • String Matching, Karp-Rabin
    • Priority Queue Interface & Implementation
    • Dictionary Problem & Implementation
    • Sorting
    • Dynamic Programming
    • Backtracking
    • Self-Balancing Tree
  • MIT - 6.824
    • 2PC & 3PC
    • Introduction and MapReduce
    • RPC and Threads
    • Primary/Backup Replication
    • Lab: Primary/Backup Key/Value Service
    • Google File System (GFS)
    • Raft
    • Lab: Raft - Leader Election
    • Lab: Raft - Log Replication
  • Stanford-CS107
    • 原始数据类型及相互转化
    • 指鹿为马
    • 泛型函数
    • 泛型栈
    • 运行时内存结构
    • 从 C 到汇编
    • 函数的活动记录
    • C 与 C++ 代码生成
    • 编译的预处理过程
    • 编译的链接过程
    • 函数的活动记录续、并发
    • 从顺序到并发和并行
    • 信号量与多线程 1
    • 信号量与多线程 2
    • 复杂多线程问题
    • 函数式编程 - Scheme 1
    • 函数式编程 - Scheme 2
    • 函数式编程 - Scheme 3
    • 函数式编程 - Scheme 4
    • 函数式编程 - Scheme 5
    • Python 基础
  • MIT - 6.001 - SICP
    • 什么是程序
    • 程序抽象
    • 替代模型
    • 时间/空间复杂度
    • 数据抽象
    • 高阶函数
    • Symbol
    • 数据驱动编程与防御式编程
    • 数据抽象中的效率与可读性
    • 数据修改
    • 环境模型
    • 面向对象-消息传递
    • 面向对象 - Scheme 实现
    • 构建 Scheme 解释器
    • Eval-Apply Loop
    • Normal Order (Lazy) Evaluation
    • 通用机
    • 寄存器机器
    • 子程序、栈与递归
    • 在寄存器机器中执行
    • 内存管理
  • MIT - 6.046
    • Randomized Algorithms
    • Skip Lists
  • System Design
    • Twitter
    • Cache Consistency & Coherence
  • DDIA 笔记
    • Replication
    • Transactions
    • The Trouble with Distributed Systems
    • Consistency & Consensus
  • Papers We Love
    • Consistent Hashing and Random Trees (1997)
    • Dynamic Hash Tables (1988)
    • LFU Implementation With O(1) Complexity (2010)
    • Time, Clocks, and the Ordering of Events in a Distributed System (1978)
    • Dapper, a Large-Scale Distributed Systems Tracing Infrastructure (2010)
    • Gorilla: A Fast, Scalable, In-Memory Time Series Database (2015)
  • Release It 笔记
    • Anti-patterns & Patterns in Microservice Architecture
  • Database Design
    • Log Structured Merge (LSM) Tree & Usages in KV Stores
    • Prometheus
Powered by GitBook
On this page
  1. Stanford-CS107

编译的预处理过程

第十二课

PreviousC 与 C++ 代码生成Next编译的链接过程

Last updated 6 years ago

一段 c 代码编译的过程按顺序实际上分为三个步骤:预处理 (preprocess)、编译 (compile) 以及链接 (link),如下图所示:

预处理可以理解成一个文本替换过程,它可以帮你将 #define 的内容进行文本替换,预处理完的文本将传递给编译器;编译器将 c 代码编译成汇编语言,接着将汇编代码传递给链接器 (linker);链接器将引用到的其它代码和编译好的代码合并成可执行程序,即编译过程的最终结果。

预处理阶段

例1:#define

// preprocessor_test_1.c
#define kwidth 40
#define kheight 80
#define kPerimeter 2*(kwidth + kheight)

int main() {
    int area = kwidth * kheight;
    return kPerimeter;
}

输入以下命令执行预处理过程:

$ clang -E preprocessor_test_1.c

得到的预处理结果如下:

int main() {
    int area = 40 * 80;
    return 2*(40 + 80);
}

预处理器会从上到下扫描输入文件,读到 #define 时,记录下 token 及其对应的文本内容,当它在代码中遇到相应的 token,如 kwidth、kheight 后,就会直接将对应的文本替换到相应位置,同时把 #define 语句去除。

例2:宏 (macro)

// preprocessor_test_2.c
#define MAX(a,b) (((a) > (b)) ? (a) : (b))

int main() {
    return MAX(3, 4);
}

输入同样的命令执行预处理过程,得到的结果如下:

int main() {
    return (((3) > (4)) ? (3) : (4));
}

预处理器也支持宏 (macro) 定义,它类似函数但没有类型定义,这是带有参数的文本替换。

例3:宏 - 文本替代详解

// preprocessor_test_3.c
#define MAX(a,b) (((a) > (b)) ? (a) : (b))

int main() {
    return MAX(3, "hello world");
}

显然,这段代码不是合法的 c 代码,int 无法与 char* 比较。虽然编译无法通过,但预处理过程依然可以执行,得到的结果如下:

int main() {
    return (((3) > ("hello world")) ? (3) : ("hello world"));
}

也印证了预编译过程仅仅做了文本替代,并没有涉及其它部分的功能。

例4:

#define NthElementAddr(base, elemSize, index) \
    ((char *)base + index * elemSize)

void *VectorNth(vector *v, int position) {
    assert(position >= 0);
    assert(position < v->loglength);
    return NthElemAddr(v->elems, v->elemSize, position);
}

在实现抽象数据类型 Vector 时,我们需要多次使用指针算术来计算一段内存的第 n 个元素地址,这种常用的计算模式就可以抽象成宏或者利用 helper function 来完成,本例就是利用宏来实现抽象。预处理的结果不再展开。

例5:

实际上,我们在防御性编程中,经常使用到的 assert,也是定义在 assert.h 中的一个宏,它的完整实现可以在类似 /usr/include/assert.h 路径中查到,这里简单介绍一下它的简单实现:

#ifdef NDEBUG
    #define assert(cond) (void)0
#else
    #define assert(cond) \
        (cond) ? ((void) 0) :
            fprintf(stderr, "..........."), exit(0)
#endif

当 NDEBUG 为 true 时,assert 被替换为 ((void)0),即 no-op,编译时会在优化的过程中去除。

需要注意的是:在使用宏时,要注意它只做文本替换,程序员需要警惕替换的结果可能造成的问题。

例6:使用宏可能出现效率问题

#define MAX(a,b) (((a) > (b)) ? (a) : (b))

int main() {
    return MAX(fib(100), fact(4000));
}

会被预处理成

int main() {
    return (fib(100) > fact(4000)) ? fib(100) : fact(4000);
}

其中 fib(100) 与 fact(4000) 中的较大一方将被执行两次。

例7: 使用宏可能出现逻辑问题

#define MAX(a,b) (((a) > (b)) ? (a) : (b))

int main() {
    return MAX(m++, n++);
}

会被预处理成

int main() {
    return (((m++) > (n++)) ? (m++) : (n++));
}

显然,m 和 n 有一个会自增两次。

例8:#include

#include <stdio.h>
#include <assert.h>
#include "vector.h"

当预处理器遇到 #include 的时候,它会去寻找对应的文件,用该文件的所有内容代替 #include 语句。当文件名用尖括号包裹的时候,预处理器会从系统路径中寻找,如 /usr/bin/include 或者 /usr/include 等等;当文件名用双引号包裹的时候,预处理器会从当前工作目录下寻找。同时,#include 语句替换是递归的,如果在读取的文件中读到 #include ,预处理器将递归地去读取对应的文件。

一般情况下,我们只在头文件 (*.h) 中声明函数原型、结构体、全局变量、宏等信息,而没有相关实现。原因在于编译后的汇编代码实际上没有声明,只有实现,因此这些声明会在编译后消失。

参考

Stanford CS107: lecture 12