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

4

24

I'm pretty sure glibc is the name of the standard C library implementation for gcc.

But for LLVM/Clang I'm not sure. I've Googled to try to find if it comes with its own implementation of the whole standard C library, or whether they also use glibc. Surprisingly, all I can find is a recent article discussing that Google is considering writing a new libc for LLVM. But not what that would replace.

Even in the LLVM/Clang source repository, there's a good chance I'm just blind or stupid, but I can't seem to find it.

Just to be clear I'm only interested in the C standard library, not the C++ standard library. I'm particularly interested in poring over their implementation of the printf family of functions.

Can somebody show me where to look for Clang's libc/standard C library implementation or its source repo?

Sortition answered 24/11, 2019 at 16:37 Comment(2)
@gman: I am assuming there's something I don't understand because the latter link only seems to have some parts of a libc. printf and friends don't seem to be there? (The first link seems to be C++ standard library which I don't need, unless of course it also includes the C library?)Sortition
Why should it have to use only one or even provide its own? What have you tried to find out?Toboggan
S
18

Clang does not come with its own C standard library. Rather, it "supports a wide variety of C standard library implementations".

Unix-like systems including Mac OS ship with their own. Windows does not. On Windows the default arrangement requires Microsoft's Visual C libraries to be installed. It seems that it's also possible to use Clang on Windows with MinGW's libraries though.

Sortition answered 24/11, 2019 at 18:37 Comment(2)
One can also use clang with Microsoft Visual Studio C and C++ libraries.Nathalia
Just a note that in 2022, this has changed and the "llvm-libc" project is a thing nowRetired
T
10

When compiling C++ code, libstdc++ from GNU, or LLVM's libc++ can be used as the C++ Standard Library. They are set by compiling with clang++ using flag -stdlib=libstdc++, or -stdlib=libc++, respectively. This information is laid out reasonably well at https://clang.llvm.org/docs/Toolchain.html.

When compiling C code, the C standard library cannot be switched per-compilation. It is necessary to build the entire compiler toolchain for the selected C library. That means either cross-compilation, or, in case of Linux, compiling in a distribution that uses the chosen C library. If I wanted to compile with musl, I'd compile in Alpine Linux, and so on.

Townswoman answered 12/5, 2020 at 8:27 Comment(0)
D
9

Things are changing! - LLVM is working on its own libc implementation https://github.com/llvm/llvm-project/tree/main/libc

They haven't talked about it in a couple of versions: 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.

Parts I find most interesting are:

  • 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.

Looks like LLVM 14 or 15 will be released with it. Watch this space for further documentation!

Determinative answered 3/3, 2022 at 18:18 Comment(1)
It now has a home page and a recent talk at CppNow.Whirlwind
B
8

But for LLVM/Clang I'm not sure

It's unclear whether you are asking about which libc clang itself is using (as in, the clang compiler binary), or whether you are asking about which libc the binary that clang produced as a result of compilation and linking is using.

In either case, the answer is: whichever libc you told it to use (either at clang configuration time, or at the time of linking the final binary).

It could be GLIBC, uClibc, Musl, Apple libc, or some other libc.

Eventually, LLVM-libc may be ready and could be used as well.

Update:

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

No: a suitable default -- system libc, which is usually GLIBC (but not always) on Linux, and Apple libc on MacOS, is provided.

I have not touched Windows in over 10 years, but I suspect that MSVCRT.DLL is eventually used by default there.

But the end-user can override the default choice by using suitable -I... and -L... flags at compile and link time.

The end user could also change the default by reconfiguring and rebuilding clang.

Bard answered 24/11, 2019 at 17:32 Comment(4)
Ah. I'm asking when I write say "Hello world" in C and compile it with Clang, which set of standard functions will be linked into the resulting executable? Are you saying that every time you install Clang the user must decide with libc it will use? I did not see that happen when I installed it in Linux, Windows, or Mac. I don't recall doing any "telling to use" of which you speak.Sortition
Thanks! It is now useful. In the '90s the C compilers I used all came with their own C library, and usually with the source.Sortition
Yes it seems from this answer on another SO question that Clang on Windows requires Microsoft Visual C's libraries.Sortition
Do you have any specific instructions about those "suitable -I... and -L... flags"? Everything I've seen so far talks only about rebuilding the compiler to use a selected c std lib.Townswoman

© 2022 - 2024 — McMap. All rights reserved.