Clang 使用什么标准 C 库? glibc,它自己的,还是其他的?

What standard C library does Clang use? glibc, its own, or some other one?

我很确定 glibc 是 gcc 的标准 C 库实现的名称。

但对于 LLVM/Clang 我不确定。我用谷歌搜索试图找出它是否带有自己对整个标准 C 库的实现,或者它们是否也使用 glibc。令人惊讶的是,我能找到的只是最近一篇讨论该 Google is considering writing a new libc for LLVM 的文章。但不会取代什么。

即使在 LLVM/Clang 源存储库中,我很可能只是瞎了眼或愚蠢,但我似乎找不到它。

需要说明的是,我只对 C 标准库感兴趣,对 C++ 标准库不感兴趣。我特别感兴趣的是研究他们对 printf 系列函数的实现。

有人可以告诉我在哪里可以找到 Clang 的 libc/standard C 库实现或其源代码库吗?

But for LLVM/Clang I'm not sure

不清楚您是在询问 clang 本身正在使用哪个 libc(如 clang 编译器二进制文件),还是在询问 哪个 libc clang 生成 的二进制 作为编译的结果并且 linking 正在使用。

在任何一种情况下,答案都是:无论您告诉它使用哪个 libc(在 clang 配置时,或者在 linking 最终二进制文件时)。

它可能是 GLIBC、uClibc、Musl、Apple libc 或其他一些 libc。

最终,LLVM-libc 可能已经准备就绪,也可以使用了。

更新:

Are you saying that every time you install Clang the user must decide with libc it will use?

否:提供了合适的默认值 -- 系统 libc,在 Linux 上通常是 GLIBC(但不总是),在 MacOS 上通常是 Apple libc。

我已经 10 多年没碰过 Windows,但我怀疑那里最终默认使用了 MSVCRT.DLL

但是最终用户 可以 通过在编译和 link 时使用合适的 -I...-L... 标志来覆盖默认选择。

最终用户还可以通过重新配置和重建来更改默认设置 clang

Clang 没有自带 C 标准库。相反,它 "supports a wide variety of C standard library implementations".

类 Unix 系统包括 Mac OS 自带。 Windows 没有。在 Windows 上,默认安排需要安装 Microsoft 的 Visual C 库。似乎也可以在 Windows 上使用 Clang 和 MinGW 的库。

在编译 C++ 代码时,GNU 的 libstdc++ 或 LLVM 的 libc++ 可以用作 C++ 标准库。它们是通过使用标志 -stdlib=libstdc++-stdlib=libc++clang++ 一起编译来设置的。此信息在 https://clang.llvm.org/docs/Toolchain.html.

中的布局合理

编译 C 代码时,C 标准库无法在每次编译时切换。有必要为所选的 C 库构建整个编译器工具链。这意味着交叉编译,或者在 Linux 的情况下,在使用所选 C 库的发行版中编译。如果我想用 musl 编译,我会在 Alpine Linux 中编译,依此类推。

情况正在发生变化! - LLVM 正在开发自己的 libc 实现 https://github.com/llvm/llvm-project/tree/main/libc

他们还没有在几个版本中谈论它:https://releases.llvm.org/12.0.0/docs/Proposals/LLVMLibC.html

llvm-libc, [is] an implementation of the C standard library targeting C17 and above, as part of the LLVM project. llvm-libc will also provide platform specific extensions as relevant. For example, on Linux it also provides pthreads, librt and other POSIX extension libraries.

我觉得最有趣的部分是:

  • Designed and developed from the start to work with LLVM tooling and testing like fuzz testing and sanitizer-supported testing.

  • Use source based implementations as far possible rather than assembly. Will try to fix the compiler rather than use assembly language workarounds.

看起来 LLVM 14 或 15 将随之发布。观看此 space 以获取更多文档!