ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I just wonder why Linux is written in C and not in C++. Is there any logical reasoning for this or its just the popularity of C which made the selectio of the language?
i guess it makes sense because unix is c, and c is probably the only practical language above assembly with which to program an operating system. though you still need some assembly in the kernel.
i'm not aware of any practical general purpose kernel that is written in any thing other than c.
also i think c++ is too dirty, or not as clean as c.
I would think it's mostly a matter of timing. If you think about it, C++ first showed on the radar screen in 1985. I am unsure when Torvalds began linux development, but at least parts surfaced in 1991. It is quite likely that he wasn't familiar with C++, or at least not enough to use it as well as the well-established C.
Cleaner? I don't know, I guess it depends what you're used to. I prefer Strings to char* but that is just a personal preference.
During the recent discussion, when it was suggested that perhaps the kernel is written in C simply because "we've always done it that way...", Linux creator Linus Torvalds joined in to explain:
"In fact, in Linux we did try C++ once already, back in 1992. It sucks. Trust me - writing kernel code in C++ is a BLOODY STUPID IDEA.
"The fact is, C++ compilers are not trustworthy. They were even worse in
1992, but some fundamental facts haven't changed: 1) the whole C++ exception handling thing is fundamentally broken. It's _especially_ broken for kernels. 2) any compiler or language that likes to hide things like memory allocations behind your back just isn't a good choice for a kernel. 3) you can write object-oriented code (useful for filesystems etc) in C, _without_ the crap that is C++."
he he he i told you c++ was dirty. personally, i think things like the dynamic/static/const... casts are dirty.
and you know if you allocate objects dynamically (using new), then you have to call delete on them to invoke the destructor. so what if you forget to delete -> memory leak, see? and then to "solve" that problem, they introduced the --what is it? i don't remember what they're called anymore-- safe pointer or something. and the safe pointers are instantiated statically, so the destructor is invoked automatically when the safe pointer goes out of scope. but then there's this issue of "the owner" of the memory that the safe pointer points to. blah blah blah. it's a mess.
I admire Linus Torvalds and George Lucas a lot (what? How could I put both in the same sentence... I will explain ). But I just don't know... sometimes they are a bit full of crap: "I've tried to program Linux with C++" and "I've planned Jar Jar Binks in the 70's", sounds suspicious.
I for one, believe that a kernel written in C++ would be still fast and easier to maintain. And just because it's C++, it does not stops you for using anything for C...
Again, I admire Linus for creating Linus (even though, the current kernel is only 2% written by him) and George Lucas for creating a huge, deep world as Star Wars (even though, his new movies with computer graphics looks horrible) but when they are talking things from the past like this, finding excuses for that... smells like Bantha podoo for me...
I can tell you from experience that C++ is a lot bloatier than an application that does the same thing in C.
It's not a question of whether or not C++ is fast. It's a question of whether or not C++ is faster than C, and if it's worth the trouble to rewrite the entire kernel (which means introducing new bugs, etc.) Linus has stated before that if they switch to a C++ kernel that they'd do it all or nothing. They wouldn't start writing little bits at a time in C++ and tie it into other C code.
Thank you all for your valuable responses and information. I think there is no strict reason why C++ is not used, but the time as C was very popular and stable at the time when Linux was developed.
Thanks all.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.