开发者

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

C and C++ by themselves can't actual开发者_开发百科ly 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++.


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜