Take the 2-minute tour ×

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

share|improve this question
I 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 8 '11 at 5:09
C and C++ as themselves can't actually do anything. @Other Folks, please don't take it so seriously !! –  iammilind Jul 8 '11 at 5:14
"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 8 '11 at 5:17
@Chris: Your strchr has a bug. It loops off the end of the string if the character is never found... –  R.. Jul 8 '11 at 5:25
@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 8 '11 at 5:33

7 Answers 7

up vote 44 down vote accepted

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

share|improve this answer
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 8 '11 at 5:25
Some asm is mandatory, not just for performance. For instance there is no way to write setjmp in C. –  R.. Jul 8 '11 at 5:27
@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 8 '11 at 5:31
This reminds me of the backdoor Ken Thompson supposedly put in the Unix C compiler: scienceblogs.com/goodmath/2007/04/15/…. –  paraxor Feb 22 '13 at 14:46

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

You might find these links useful:

share|improve this answer
Don't forget Lib C++, which is designed for C++0x. –  Nicol Bolas Jul 8 '11 at 5:10

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.

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer
Okay, then what was the first C compiler written in? –  alecwhardy Jul 8 '11 at 5:28

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.

share|improve this answer

Your Answer


