Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 213
hippietrail Avatar asked Nov 24 '19 16:11

hippietrail


4 Answers

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.

like image 192
hippietrail Avatar answered Oct 20 '22 14:10

hippietrail


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!

like image 35
Samuel Marks Avatar answered Sep 19 '22 23:09

Samuel Marks


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.

like image 8
user7610 Avatar answered Oct 20 '22 12:10

user7610


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.

like image 7
Employed Russian Avatar answered Oct 20 '22 12:10

Employed Russian