1
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
School of Computer Science and Technology, HIT
第7章 链接
计算机科学与技术学院
哈尔滨工业大学
2
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
School of Computer Science and Technology, HIT
要点
链接
案例学习: 库打桩机制
3
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
School of Computer Science and Technology, HIT
一个典型程序的转换过程
4
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
School of Computer Science and Technology, HIT
C程序例子
int sum(int *a, int n);
int array[2] = {1, 2};
int main()
{
int val = sum(array, 2);
return val;
}
int sum(int *a, int n)
{
int i, s = 0;
for (i = 0; i < n; i++) {
s += a[i];
}
return s;
}
main.c
sum.c
每个模块都有自己的代码、数据(初始化全局变量、未初始化全
局变量、静态变量、局部变量)
5
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
School of Computer Science and Technology, HIT
静态链接
使用编译器驱动程序compiler driver进行程序的翻译和链接:
linux> gcc -Og -o prog main.c sum.c
linux> ./prog
Linker (ld)
Translators
(cpp, cc1, as)
main.c
main.o
Translators
(cpp, cc1, as)
sum.c
sum.o
prog
源程序
分开编译成可重定位目标文件
完全连接的可执行的目标文件
(
包括
main.c
与
sum.c
定义的所有函数的代码和数据
)