C and C++ by themselves can't actually do anything, they need the libraries to work. So how were the libraries created? Assembly language?
-
5I think you'd have to qualify which compiler and version you're asking about. Many compilers can compile themselves and even then the source may be a mix of languages. – SpliFF Jul 08 '11 at 05:09
-
9`C and C++ as themselves can't actually do anything`. @Other Folks, please don't take it so seriously !! – iammilind Jul 08 '11 at 05:14
-
9"C and C++ as themselves can't actually do anything," Bah! Here are some library functions, written in pure C with no standard library: `char *strchr(const char *c, int i) { while(*c != (char)i)) c++; return (char *)c; }` `size_t strlen(const char *c) { return (size_t)(strchr(c, 0) - c); }` – Chris Lutz Jul 08 '11 at 05:17
-
2@Chris: Your `strchr` has a bug. It loops off the end of the string if the character is never found... – R.. GitHub STOP HELPING ICE Jul 08 '11 at 05:25
-
3@R.. - I didn't say they were correct... ;) `char *strchr(const char *c, int i) { for(; *c; c++) if(*c == (char)i) return c; return NULL; }` – Chris Lutz Jul 08 '11 at 05:33
-
1@Chris Lutz you could add a simple `*c &&` instead of rewriting whole thing – Ulterior Jul 10 '11 at 10:58
-
@user757808 - I'd still need a way to differentiate between ending because the string ended vs. ending because the character was found. Whih would require not-insignificant rewrites for the case where `i == '\0'`. – Chris Lutz Jul 10 '11 at 16:23
-
It seems like most answers are missing the point of this question. “C itself provides no input-output facilities. There are no READ or WRITE statements, and no built-in file access methods.” [This quote is from the Introduction to K&R C.] If the standard library is written in C, then how does the standard library do these things? *That* is the point of confusion which is being glossed over here. – littleO Jun 26 '22 at 00:13
6 Answers
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++.

- 65,341
- 71
- 269
- 466
-
This is correct. A compiler need not be self hosting in order to compile a standard library. Once you have a standard library, you can make a self hosting compiler. – Tim Post Jul 08 '11 at 05:25
-
3Some asm is mandatory, not just for performance. For instance there is no way to write `setjmp` in C. – R.. GitHub STOP HELPING ICE Jul 08 '11 at 05:27
-
1@R..: I agree there are a few instances where C is not the appropriate language for some tasks. However, these are a very small percent of the overall library. – Jonathan Wood Jul 08 '11 at 05:31
-
1This reminds me of the backdoor Ken Thompson supposedly put in the Unix C compiler: http://scienceblogs.com/goodmath/2007/04/15/strange-loops-dennis-ritchie-a/. – Feb 22 '13 at 14:46
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.

- 93,612
- 16
- 138
- 200
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.

- 202,538
- 53
- 430
- 533
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.

- 23,114
- 6
- 54
- 68
-
1
-
2@alecwhardy: http://www.quora.com/What-language-was-the-first-C-compiler-written-in – Jonathan Wood Jul 08 '11 at 06:02
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.

- 476,176
- 80
- 629
- 1,111
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.

- 1
- 1

- 46,512
- 18
- 65
- 82