简体   繁体   English

Linux 与 Windows 中的系统库

[英]System libraries in Linux vs. Windows

My background is in Windows and I'm a Linux noob.我的背景是 Windows,我是 Linux 菜鸟。 Still trying to wrap my head around some basic concepts, and specifically the system libraries:仍在尝试围绕一些基本概念,特别是系统库:

  1. Windows has ntdll.dll which wraps system calls, and a CRT dll which interface between the C syntax to the ntdll OS-exposed services. Windows has ntdll.dll which wraps system calls, and a CRT dll which interface between the C syntax to the ntdll OS-exposed services.
    (for simplification I ignore the intermediate layer of user32, kernel32, kernalbase etc. I also realize the CRT is several dlls, this is not the point). (为简化起见,我忽略了 user32、kernel32、kernalbase 等中间层。我也意识到 CRT 是几个 dll,这不是重点)。
  2. It seems Unix/Linux has pretty much just libc, which wraps system calls and called directly from your application code.似乎 Unix/Linux 几乎只有 libc,它包装系统调用直接从您的应用程序代码中调用。

Is this the right analogy?这是正确的比喻吗? (ntdll + CRT) <===> libc? (ntdll + CRT) <===> libc?

I realize that C & Unix evolved together, but am still surprised.我意识到 C 和 Unix 一起进化,但我仍然感到惊讶。 Can it be that the C interface is hard-wired into the OS for Unix/Linux?是不是 C 接口硬连线到 Unix/Linux 的操作系统中? In Windows non-C programs link against the underlying OS provided dlls.在 Windows 中,非 C 程序链接到底层操作系统提供的 dll。 Is it possible that in Linux there is no OS/C-runtime border?在 Linux 中是否可能没有 OS/C 运行时边界?

In general, most programs link against libc, even if they are written in another language.通常,大多数程序都链接到 libc,即使它们是用另一种语言编写的。 It provides the C standard library interface (like MSVCRT), POSIX features (the equivalent of certain parts of the Win32 subsystem), and wrappers around system calls.它提供 C 标准库接口(如 MSVCRT)、POSIX 功能(相当于 Win32 子系统的某些部分)和围绕系统调用的包装器。 For example, Rust uses libc because it provides a portable environment to link against.例如,Rust 使用 libc,因为它提供了一个可移植的链接环境。

However, on Linux, you don't have to link against libc.但是,在 Linux 上,您不必链接 libc。 Go chooses to make system calls directly, which means it can ship static binaries that have no runtime dependencies. Go 选择直接进行系统调用,这意味着它可以发布没有运行时依赖的 static 二进制文件。 This is possible because Linux guarantees a stable kernel ABI, but not all operating systems do this (eg, macOS).这是可能的,因为 Linux 保证了稳定的 kernel ABI,但并非所有操作系统都这样做(例如,macOS)。 So unless you have significant resources (like an entire programming language team), this isn't generally a smart move unless you're only working with a handful of syscalls.因此,除非您拥有大量资源(例如整个编程语言团队),否则这通常不是明智之举,除非您只使用少数系统调用。

I should point out that even Windows is intrinsically wired into the C language: it uses C strings (granted, usually wide C strings) for its system calls, and much of the kernel is written in C. I should point out that even Windows is intrinsically wired into the C language: it uses C strings (granted, usually wide C strings) for its system calls, and much of the kernel is written in C. Even if you were starting a kernel from scratch, you'd still need a general C interface, because virtually every programming language has a way to interact with C.即使您从头开始使用 kernel,您仍然需要一个通用的 C 接口,因为几乎每种编程语言都有与 C 交互的方式。

The Linux system calls are documented in syscalls(2) and are the foundation of user-land programs. Linux系统调用记录在syscalls(2)中,是用户级程序的基础。 The calling conventions are documented in the ABI specifications.调用约定记录在ABI规范中。 The ELF executable format is documented, eg in elf(5) .ELF可执行格式已记录在案,例如在elf(5)中。

Read also Advanced Linux Programming and about the Unix philosophy .另请阅读高级 Linux 编程Unix 理念

You can make system calls directly in assembler.您可以直接在汇编程序中进行系统调用。 The Linux Assembly HowTo explains that. Linux 组装方法说明了这一点。 You'll prefer to use the C interface, and for that reason the libc is preferable.您会更喜欢使用 C 接口,因此最好使用libc In practice, the libc.so is the cornerstone of most Linux systems.实际上, libc.so是大多数 Linux 系统的基石。

Play with ldd(1) , pmap(1) , strace(1) , BusyBoxldd(1)pmap(1)strace(1)BusyBox

The GCC compiler enables useful language extensions , and mixing C and assembler code. GCC编译器支持有用的语言扩展,并混合 C 和汇编代码。

Some programming languages implementations are barely using C and could call system calls directly (look into SBCL or Go ...)一些编程语言实现几乎没有使用 C 并且可以直接调用系统调用(查看SBCLGo ...)

Both the Linux kernel and usual GNU libc (or musl-libc ), and also the GCC compiler and the binutils are free software or open source , and you can study their source code . Linux kernel和通常的GNU libc (或musl-libc ),以及GCC编译器和binutils代码都是免费的,

Things become trickier with systemd and vdso(7) .使用systemdvdso(7)事情变得更加棘手。

See also http://linuxfromscratch.org/另见http://linuxfromscratch.org/

Graphical applications are using some display server , often Xorg orWayland .图形应用程序使用一些显示服务器,通常是XorgWayland Read about X11 .阅读有关X11的信息。 You would want to use GUI toolkits like GTK or Qt to code them.您可能希望使用GTKQt 之类的 GUI 工具包对其进行编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM