Linux 与 Windows 中的系统库

System libraries in Linux vs. Windows

我的背景是 Windows,我是 Linux 菜鸟。仍在努力思考一些基本概念,特别是系统库:

  1. Windows 具有包装系统调用的 ntdll.dll 和一个 CRT dll C 语法与 ntdll OS-exposed 之间的接口 服务。
    (为简化起见,我忽略了中间层 user32、kernel32、kernalbase 等。我也意识到 CRT 是几个 dll,这不是重点)。
  2. 似乎 Unix/Linux 几乎只有 libc,它包装了直接从您的应用程序代码调用的系统调用

这个比喻正确吗? (ntdll + CRT) <===> libc ?

我知道 C 和 Unix 是一起进化的,但还是很惊讶。难道 C 接口是硬连接到 OS 的 Unix/Linux 吗?在 Windows 非 C 程序中 link 针对底层 OS 提供的 dll。有没有可能Linux里面没有OS/C-runtime边框?

一般来说,大多数程序 link 都是针对 libc 的,即使它们是用另一种语言编写的。它提供 C 标准库接口(如 MSVCRT)、POSIX 特性(相当于 Win32 子系统的某些部分)以及系统调用的包装器。例如,Rust 使用 libc 是因为它为 link 提供了一个可移植的环境。

但是,在 Linux 上,您 不必 link 对抗 libc。 Go 选择直接进行系统调用,这意味着它可以发布没有运行时依赖性的静态二进制文件。这是可能的,因为 Linux 保证了稳定的内核 ABI,但并非所有操作系统都这样做(例如 macOS)。因此,除非您拥有大量资源(例如整个编程语言团队),否则这通常不是明智之举,除非您只处理少数系统调用。

我应该指出,即使 Windows 本质上也连接到 C 语言中:它使用 C 字符串(当然,通常是宽 C 字符串)进行系统调用,并且大部分内核都是用 C 编写的. 即使您是从头开始编写内核,您仍然需要一个通用的 C 接口,因为几乎每种编程语言都有一种与 C 交互的方式。

Linux system calls are documented in syscalls(2) and are the foundation of user-land programs. The calling conventions are documented in the ABI specifications. The ELF executable format is documented, e.g. in elf(5).

另请阅读Advanced Linux Programming and about the Unix philosophy

您可以直接在汇编程序中进行系统调用。 Linux Assembly HowTo 对此进行了解释。您会更喜欢使用 C 接口,因此 libc 更可取。实际上,libc.so 是大多数 Linux 系统的基石。

ldd(1), pmap(1), strace(1), BusyBox

一起玩

GCC compiler enables useful language extensions, and mixing C and assembler代码。

一些编程语言实现几乎不使用 C,可以直接调用系统调用(查看 SBCL or Go ...)

Linuxkernel and usual GNU libc (or musl-libc), and also the GCC compiler and the binutils are free software or open source,您可以研究他们的源代码

systemd and vdso(7) 的事情变得更加棘手。

另见 http://linuxfromscratch.org/

图形应用程序正在使用一些 display server, often Xorg or Wayland. Read about X11. You would want to use GUI toolkits like GTK or Qt 对其进行编码。