views:

809

answers:

8

Why are Linux, MacOS and other operating systems written in C and not in C++? Isn't C++ C with new features?

+5  A: 

C++ used to be "c + new features". But that was a long time ago now. Of course, it depends to some extent on your C++ compiler, but the language has evolved and now contains important syntactical differences as well. "c + new features" over-simplifies the situation.

But your question premise is also mistaken - linux and OS X reportedly both have significant amounts of not only C code but also both C++ and raw assembly in them.

Joel Coehoorn
neither Linux nor MacOS have any C++ in the kernel
Javier
@Javier os > kernel
Joel Coehoorn
linux = kernel.. Sigh this old discussion. Gnu/Linux/Kde whatever = OS
Esben Skov Pedersen
even if you count the GNU toolset i'd be surprised to find any C++ there. X11 and xlib are pure C too. only when you reach the desktop manager level you find C++ (Qt/KDE) or Mono (Gnome, but even GTK is still pure C), in neither case you can consider that part of the OS, specially when both desktops work on many different OS's
Javier
@Javier: the gcc folk recently voted to allow c++ code to be used in gcc. So while I think you are correct historically...times they are-a changing.
Evan Teran
+10  A: 

Question: "What new features of c++ do you think would make it possible to develop a faster or more secure or better optimized or <other cool feature> OS?"

Second question: "What major OS has been developed since c++ really came of age?"

(Note that one possible answer is Be, which I think did use c++... Ah, wikipedia says the API was c++, which is not quite the same thing.)

dmckee
Q2: Symbian....
Ben Voigt
@Ben: Good answer, and a counter example to the OP's premise. I didn't think of it because I'm a dinosaur: I work on machines with proper keyboards and enough horsepower to run an interesting Monte Carlo in less than a week.
dmckee
So do I, but I'm also a geek who associates with geeks. In my group of 6 engineers at work I think we have WM, Blackberry, Symbian, Android, and Apple devices.
Ben Voigt
+10  A: 

The very short answer is that no entirely new operating systems of any significance have been created at all since C++ attained maturity in the late 90's. And using C++ is not, by itself, nearly a compelling enough reason to completely re-write an existing successful OS.

Tyler McHenry
What about iOS?
Joel Coehoorn
iOS originated as a stripped-down version of Apple's OS, which in turn was based on Unix. So no, it's not a completely new OS. The other smartphone OSes could be, but most of them are based off of Linux in one way or another - correct me if I'm wrong though.
Cthulhu
+9  A: 

If you visit osdev.org, you'll find that many people do in fact use c++ for OS development. In fact there are pages in the wiki specifically dedicated towards demonstrating the basics of doing so. Lots of hobbyist operating systems (my own included) are written in c++ with a decent degree of success as far as functionality goes.

However, major operating systems such as windows, linux and osx were largely written before c++ was considered mature. It doesn't make sense to retrofit code to support a new language (though to be honest, it isn't difficult to get the majority of c++ supported in kernel land, exceptions and rtti are the only real annoyances).

Some people may say that since it is not trivial to get rtti and exceptions in kernel land, then there isn't a large advantage to using c++. i disagree, c++ is a great language to use for kernel development. templates are an amazing tool, for writing efficient generic code. Even things such as a type safe linked lists are much easy to implement using c++ without the need for anything but templates. Also, many things in kernels are inherently an object oriented task, while you can clearly model these things with c well, why not use a language that more easily models them. These are just a few of the advantages.

Evan Teran
+2  A: 

Initially, C++ is not used for performance reason. At this point, all these OS have been developed on C + asm and rewrite to C++ to no one will. Also C++ is not so obvious as C. In particular because of this, Linus Torvals doesn't like C++. http://thread.gmane.org/gmane.comp.version-control.git/57643/focus=57918

Alexander
A: 

As far as I know, almost all Haiku OS is made of C++ : http://en.wikipedia.org/wiki/Haiku_(operating_system)

Klaim
+9  A: 

C++ is used widely for operating systems. Big parts of Windows is written in it.

But the OS isn't one monolithic blob. There are different layers and components to it. A few bits and pieces pretty much have to be written in assembly, because there is no portable way to implement them: this is low-level handling of the CPU and other hardware, which is abstracted away in higher level programming languages.

Then there is a layer which can be written in a higher level language, but it just can't rely on other code. It provides a few basic services that are needed for the next layer, and so it has to just work on its own.

C++ can be used there, but there are some limitations. C++ requires the runtime library to implement quite a bit of functionality (exception handling, for example), which might not be available (or which it might not be desirable to make available at that level), and so C may be a better solution there. (Although an alternative option is to use C++, but simply avoid features that rely on this missing functionality)

But the further out you get, the more abstractions and OS services and runtime libraries can be made available, and the more high level languages become suitable.

So C++ can be, and is used widely in operating systems. Just not at every level.

jalf
+1 for giving an answer with (a little) insight into how an OS is made.
jhominal
A: 

My understanding is that eCos was written entirely in C++ and provides C interfaces.

Khadaji