- Hands-On System Programming with C++
- Dr. Rian Quinn
- 375字
- 2021-07-02 14:42:32
All about linking
Linking is an extremely complex topic that varies from operating system to operating system. For example, Windows links programs quite differently to Linux. For this reason, we will limit our discussion to Linux.
When a C source file is compiled, it is compiled into what is called an object file, which contains the compiled source code with each function that is defined in the program in a binary format, as follows:
> gcc -c scratchpad.c; objdump -d scratchpad.o
...
0000000000000000 <main>:
0: 55 push %rbp
1: 48 89 e5 mov %rsp,%rbp
4: 48 8d 3d 00 00 00 00 lea 0x0(%rip),%rdi # b <main+0xb>
b: e8 00 00 00 00 callq 10 <main+0x10>
10: b8 00 00 00 00 mov $0x0,%eax
15: 5d pop %rbp
16: c3 retq
As shown here, the compiler creates an object file, which contains the compiler (that is, binary) version of the source code. An important note here is that that the main() function is labeled main, in plain text.
Let's expand this example to include another function:
int test(void)
{
return 0;
}
int main(void)
{
return test();
}
Compiling this source, we get the following:
> gcc -c scratchpad.c; objdump -d scratchpad.o
...
0000000000000000 <test>:
0: 55 push %rbp
1: 48 89 e5 mov %rsp,%rbp
4: b8 00 00 00 00 mov $0x0,%eax
9: 5d pop %rbp
a: c3 retq
000000000000000b <main>:
b: 55 push %rbp
c: 48 89 e5 mov %rsp,%rbp
f: e8 00 00 00 00 callq 14 <main+0x9>
14: 5d pop %rbp
15: c3 retq
As shown here, each function that is compiled is labeled using the same name as the function. That is, the name of each function is not mangled (unlike in C++). Name mangling will be explained in further detail in the next section, as well as why this is important with respect to linking.
Going beyond a simple source file, a C program is split into groups of source files that are compiled and linked together. Specifically, an executable is the combination of object files and libraries. Libraries are a combination of additional object files, divided into two different types:
- Static libraries: libraries that are linked at compile time
- Dynamic libraries: libraries that are linked at load time
- 數(shù)據(jù)庫(kù)技術(shù)與應(yīng)用教程(Access)
- 正則表達(dá)式必知必會(huì)
- Oracle RAC 11g實(shí)戰(zhàn)指南
- Python數(shù)據(jù)分析:基于Plotly的動(dòng)態(tài)可視化繪圖
- 企業(yè)級(jí)數(shù)據(jù)與AI項(xiàng)目成功之道
- 網(wǎng)站數(shù)據(jù)庫(kù)技術(shù)
- 大數(shù)據(jù)精準(zhǔn)挖掘
- 數(shù)據(jù)分析師養(yǎng)成寶典
- 數(shù)據(jù)挖掘與機(jī)器學(xué)習(xí)-WEKA應(yīng)用技術(shù)與實(shí)踐(第二版)
- Cognitive Computing with IBM Watson
- ECharts數(shù)據(jù)可視化:入門、實(shí)戰(zhàn)與進(jìn)階
- ORACLE 11g權(quán)威指南
- Trino權(quán)威指南(原書第2版)
- Flume日志收集與MapReduce模式
- 數(shù)據(jù)可視化五部曲