Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions docs/dev/library.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
---
icon: simple/cplusplus
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

换个图标,和 C/C++ 的重复了

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

material/book 或者 material/book-cog 不错,material/bookmark-box 也行

---

# 链接库
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

主标题名称需与 nav 里面一致。


!!! note "主要作者"

[@luojh][luojh]

!!! warning "本文编写中"

!!! comment "适用范围"

本文介绍 Linux 上的**静态链接库** (一般为 `.a` 文件) 和**动态链接库** (一般为 `.so` 文件)。注意:这里的链接库是指包含了可执行二进制代码的库,并不是头文件 (Header file)、Python 库等。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

括号内包含中文的情况下使用全角括号,下面其他的也一样。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Header file 不需要大写,header file 就够了


## 总览

库 (Library),是包含**可重用代码和数据**的模块。

### 构建链接库

下图给出了一般的可执行文件 (executable)、静态链接库 (static library)、动态链接库 (dynamic library) 的构建过程。

```mermaid
flowchart TD
source["源文件 (*.c, *.cpp)"] -->|编译| target["目标文件 (*.o)"]
header["头文件 (*.h)"] --- source
target -->|链接| executable["可执行文件 (ELF等)"]
target -->|ar rcs| staticlib["静态链接库 (*.a)"]
target -->|gcc -shared| dynamiclib["动态链接库 (*.a)"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
target -->|gcc -shared| dynamiclib["动态链接库 (*.a)"]
target -->|<code>gcc -shared</code>| dynamiclib["动态链接库 (*.a)"]

上面的 ar rcs 也可以这么做。

```

### 使用链接库

下图给出了**使用链接库**的方式。

```mermaid
flowchart TD
source["源文件 (*.c, *.cpp)"] -->|编译| target["目标文件 (*.o)"]
header["头文件 (*.h)"] --- source
target --> linker["链接器"]
staticlib["**静态链接库** (链接步骤加入)"] --> linker
linker --> executable["可执行文件 (ELF等)"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
linker --> executable["可执行文件 (ELF等)"]
linker --> executable["可执行文件(ELF 等)"]

dynamiclib["**动态链接库** (运行时加载)"] --> executable
```

## 源代码

对于 C 程序而言,链接库的源代码就是普通函数、变量之类,并无太多特殊要求。例如,创建并进入目录 `lib`,在 `square.c` 源文件中写一个函数 `square`

```c
int square(int x)
{
return x * x;
}
```
对应的,需要在头文件 `square.h` 中加入这个函数的原型 (prototype)
```c
int square(int x);
```

以便其他使用链接库的程序**知道如何使用这个函数** (即使这些程序不知道函数内部的实现)。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有必要讲一下预处理。


因为链接库不是完整的、可以独立运行的程序,因此不需要入口点 (比如 `main` 函数)。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

介绍一下 extern

## 静态链接库

静态链接库是在**链接步骤就加入**的链接库类型。

### 构建

首先按照正常方法编译得到 `.o` 文件:

```shell
gcc square.c -c -o square.o
```

然后使用 `ar` 程序创建静态链接库 `libsquare.a`

```shell
ar rcs libsquare.a square.o
```

注意这里的 `libsquare.a` 是命名惯例:一般静态链接库的文件名需要为 `lib<名称>.a`

到这里就完成了静态链接库的创建。

### 使用

另一个 C 程序 `main.c` 中使用了 `square` 函数,那么首先需要包含 `square.h` 头文件,这样才能知道这个函数的原形,然后就可以正常调用了。

```c
#include <main.c>
#include "lib/square.h"

int main(void)
{
int a;
scanf("%d", &a);

printf("%d^2 = %d\n", a, square(a));
return 0;
}
```
下面的命令可以将这个程序直接编译 - 链接到可执行文件 `main` (也可以分开成单独的编译步骤、链接步骤)。
```shell
gcc main.c -L./lib -lsquare -o main
```

这里,`-L./lib` 表示要求链接器在 `./lib` 中寻找链接库,`-lsquare` 表示需要链接 `libsquare.a` 这个头文件。

编译好之后,就可以正常使用了。由于静态链接库中的代码会被直接合并到链接产生的可执行文件 `main` 中,因此运行时不需要文件 `libsquare.a`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

frankly 读到这里结束我稍微有点意外,更期待一点 in-depth 的内容……

目前这个内容量大概可以作为 101 的拓展内容……?如果介绍一些 LD_LIBRARY_PATH(例如不同 CUDA 版本等)/打包相关的也许更适合放在 201 一些。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

现在这个分量确实是不够的

1 change: 1 addition & 0 deletions includes/authors.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@
[zeyugao]: https://github.com/zeyugao
[relic-yuexi]: https://github.com/relic-yuexi
[Jerry-Kwan]: https://github.com/Jerry-Kwan
[luojh]: https://github.com/luojh
117 changes: 59 additions & 58 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,63 +96,64 @@ nav:
- 章节编写指导: spec/writing.md
- 贡献者: spec/contributors.md
- 运维基础:
- ops/index.md
- 检查单: ops/checklist.md
- 服务器介绍: ops/server.md
- 包管理系统: ops/package.md
- 服务与日志管理: ops/service.md
- 存储系统:
- ops/storage/index.md
- 基础知识简介: ops/storage/intro.md
- 分区与文件系统: ops/storage/filesystem.md
- LVM: ops/storage/lvm.md
- RAID: ops/storage/raid.md
- 网络存储系统: ops/storage/network.md
- 备份与文件传输工具: ops/storage/backup.md
- ZFS: ops/storage/zfs.md
- 网络系统:
- ops/network/index.md
- 基础知识简介: ops/network/intro.md
- 网络服务实践:
- ops/network-service/index.md
- 网络时间同步: ops/network-service/ntp.md
- 隧道组网: ops/network-service/intranet.md
- Nginx: ops/network-service/nginx.md
- Zeroconf: ops/network-service/zeroconf.md
- 数据库:
- ops/database/index.md
- 关系型数据库: ops/database/rdbms.md
- 键值数据库: ops/database/kv.md
- 问题调试: ops/debug.md
- 虚拟化技术:
- ops/virtualization/index.md
- 基础知识简介: ops/virtualization/intro.md
- 容器: ops/virtualization/container.md
- QEMU/KVM: ops/virtualization/qemu-kvm.md
- 用户账户系统: ops/accounts.md
- 指标监控与告警:
- ops/monitor/index.md
- 本机指标收集: ops/monitor/local.md
- Telegraf + InfluxDB: ops/monitor/telegraf.md
- Prometheus: ops/monitor/prometheus.md
- Grafana: ops/monitor/grafana.md
- 安全: ops/security.md
- ops/index.md
- 检查单: ops/checklist.md
- 服务器介绍: ops/server.md
- 包管理系统: ops/package.md
- 服务与日志管理: ops/service.md
- 存储系统:
- ops/storage/index.md
- 基础知识简介: ops/storage/intro.md
- 分区与文件系统: ops/storage/filesystem.md
- LVM: ops/storage/lvm.md
- RAID: ops/storage/raid.md
- 网络存储系统: ops/storage/network.md
- 备份与文件传输工具: ops/storage/backup.md
- ZFS: ops/storage/zfs.md
- 网络系统:
- ops/network/index.md
- 基础知识简介: ops/network/intro.md
- 网络服务实践:
- ops/network-service/index.md
- 网络时间同步: ops/network-service/ntp.md
- 隧道组网: ops/network-service/intranet.md
- Nginx: ops/network-service/nginx.md
- Zeroconf: ops/network-service/zeroconf.md
- 数据库:
- ops/database/index.md
- 关系型数据库: ops/database/rdbms.md
- 键值数据库: ops/database/kv.md
- 问题调试: ops/debug.md
- 虚拟化技术:
- ops/virtualization/index.md
- 基础知识简介: ops/virtualization/intro.md
- 容器: ops/virtualization/container.md
- QEMU/KVM: ops/virtualization/qemu-kvm.md
- 用户账户系统: ops/accounts.md
- 指标监控与告警:
- ops/monitor/index.md
- 本机指标收集: ops/monitor/local.md
- Telegraf + InfluxDB: ops/monitor/telegraf.md
- Prometheus: ops/monitor/prometheus.md
- Grafana: ops/monitor/grafana.md
- 安全: ops/security.md
- 开发速查手册:
- dev/index.md
- 编程语言概览:
- dev/language/index.md
- 前端简介: dev/language/frontend.md
- Python: dev/language/python.md
- C/C++ 与构建工具: dev/language/cxx.md
- Shell 脚本: dev/language/shell.md
- Golang: dev/language/golang.md
- 版本管理与合作: dev/git.md
- SSH 使用技巧: dev/ssh.md
- dev/index.md
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要修改这个文件的 indentation

- 编程语言概览:
- dev/language/index.md
- 前端简介: dev/language/frontend.md
- Python: dev/language/python.md
- C/C++ 与构建工具: dev/language/cxx.md
- Shell 脚本: dev/language/shell.md
- Golang: dev/language/golang.md
- 版本管理与合作: dev/git.md
- SSH 使用技巧: dev/ssh.md
- Linux 上的链接库: dev/library.md
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

层级需在「编程语言概览」下面,预期是在 cxx.md 里面。现在的内容量没法单列一个 page。

- 高级内容:
- advanced/index.md
- CUDA 环境简介: advanced/cuda.md
- Linux 桌面与窗口系统: advanced/desktop.md
- DAC 与 MAC: advanced/dac-mac.md
- Caddy: advanced/caddy.md
- Nmap: advanced/nmap.md
- ELK: advanced/elk.md
- advanced/index.md
- CUDA 环境简介: advanced/cuda.md
- Linux 桌面与窗口系统: advanced/desktop.md
- DAC 与 MAC: advanced/dac-mac.md
- Caddy: advanced/caddy.md
- Nmap: advanced/nmap.md
- ELK: advanced/elk.md
Loading