简体   繁体   中英

What language are the C and C++ standard libraries written in?

C and C++ by themselves can't actually do anything, they need the libraries to work. So how were the libraries created? Assembly language?

C and C++ libraries are almost universally written in C and C++, as are C and C++ compilers. In fact, many compilers are even used to compile themselves!

How is this possible? Well, obviously the first C compiler couldn't have been initially developed in C. However, once a C compiler exists, then it can be used to compile another compiler. And as a compiler is being developed, so is the source code. It's possible to develop both side-by-side. Since most compilers are improvements on their predecessors, they are often used to compile better versions of themselves!

However, with respect to the library, that's easy: C can actually do something. While some lower-level routines may be written in assembler, the vast majority can be written in C or C++.

Why don't you look into an Open Source implementation of these languages and figure out?

You might find these links useful:

The standard libraries are typically written in C and C++, using a bare minimum of assembly code in order to interact with the functionality provided by the operating system, and most operating systems are written in C as well as a mix of assembly for a handful of things that cannot be done directly in C.

A more specific example...

For GNU/Linux the standard libraries are written and C and C++. For the various things that require the use of the kernel, there is ultimately an invocation of syscall , which provides the small bit of assembly code needed to jump into the kernel, where code written in a mix of C and assembly handle the call.

They are written in their host language for the simple reason that they need to interact with the operating system to perform operations they do cannot do on their own, they would do so using the operating system provided API.

The C++ Standard library is written in C++ because most of its implementation uses templates.

There's a slight misunderstanding here: The compiler is responsible for translating C or C++ (or anything else) into machine code. The libraries themselves can be written in C, there's no problem with that. Moreover, even the compiler itself can be written in C as long as there exists at least one C compiler to compile it. (The big joke is that to "properly" install gcc on linux, you need gcc to compile it from source.)

You could ask "what was the first C compiler written in", perhaps.

In a typical case, the C standard library is written primarily in C, and the C++ standard library primarily in C++.

To give some concrete numbers, Microsoft's standard library has ~1050 C and C++ files, and 37 assembly language files. Just glancing at them, I'd say at least half those assembly files could be written in C or C++ as well; they're in assembly language for the sake of optimization, not out of necessity.

Most compilers for C and C++ are written in C and C++. This is possible because of compiler bootstrapping . There is a related Stackoverflow question on the topic:

Bootstrapping a compiler: why?

Also, you might enjoy Ken Thompson's Reflection on Trusting Trust . In that paper, Thompson talks about the difficulties inherent in trusting compiled code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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