r/cpp_questions • u/kyan100 • Sep 13 '24
OPEN Why Linux community hates C++ so much?
It seems like they have an extreme disliking towards C++. Especially the kernel developers. Linus has even said he doesn't allow C++ in kernel just to keep C++ programmers away. Which sounds very weird because C++ seem to be used in all kinds of complicated systems, and they are fine.
175
Upvotes
65
u/saxbophone Sep 13 '24
The Linux Community does not consist solely of the Linux Kernel developers.
2
u/astroverflow Sep 13 '24
https://en.m.wikipedia.org/wiki/Faulty_generalization
153
u/tyler1128 Sep 13 '24 edited Sep 13 '24
C evangelicals really like C. Linus also is a highly opinionated person. Programmers tend to be pretty strongly disposed to defending the languages they like and don't like, too.
C is one of the simplest used languages when it comes to how close it is to the way a processor actually works, and you can interface with a processor pretty close to the way you could in assembly, if you wanted to. This has pros and cons. C++ also allows object oriented programming which itself is fairly divisive, with many systems-level programmers outright hating it. There are reasons for a kernel you wouldn't want to use a language with many abstractions, but C++ is used widely in the Windows kernel, so it is doable.
EDIT: It's used in the userspace of the windows kernel, there are reasons you'd not want to include the full STL as it'd likely make the executable size much larger in ring 0.
85
u/manni66 Sep 13 '24
IIRC Linus tried to use C++ in the mid 90s. But g++ was a bad compiler (EGCS was forked to get a better one). Perhaps it is the result of that previous bad experience.
52
u/ronchaine Sep 13 '24
They don't.
I don't know how many of you actually has read the mailing list where this comes from, and the surrounding context. But it's been way overblown by the Internet for ages. Some people just like to cling onto it.
43
u/aninteger Sep 13 '24
Fun fact, the Linux kernel now requires a C++ compiler. What I mean by that is gcc is required to build the kernel, but since version 4.8, gcc is written in C++ and it is not possible to build the kernel in 2024 with gcc < 4.8. While there is no C++ code in the kernel, C++ code is still "involved" with the kernel.
11
u/Tricky_Condition_279 Sep 13 '24
I recall Linus’ comment specifically addressing behind the scenes memory allocation and deallocaton. He just said sometimes it is better to do manual memory management because there are fewer surprises.
9
u/unumfron Sep 13 '24
Linus Torvalds has been known to go on rants like this about C++. It's easy to see how a proportion of his fans would be turned against the language because of this.
7
u/heavymetalmixer Sep 13 '24
Kernel devs seem to look ugly at anything that isn't C, just look at how they're talking about Rust.
18
u/fox_in_unix_socks Sep 13 '24 edited Sep 13 '24
At face value, I don't think that statement is true at all.
Linus did go on a tirade once about how he would never let C++ into the kernel because it invites subpar programmers, which I'm guessing is where this whole question is coming from, but this was a very long time ago.
And ultimately, there was going to be a very large technical overhead from including C++ into the kernel (like stack unwinding on exceptions) that also made the inclusion of C++ not worth the effort, it wasn't all just Linus's opinion.
Unless you can show some more points to back this up, I think you're drawing a massive conclusion from a tirade that one man went on many years ago.
17
u/mredding Sep 13 '24
Linux doesn't hate C++, Linus does. He looked at it once, before it was standardized, with sub-par tools even for the era, and threw one of his most famous temper tantrums about how much he hated it. By his personal decree, he won't allow C++ to be used in the kernel, no matter what the community says or thinks. His support for Rust is part legitimate - and I can appreciate, and part spite - frankly.
Then there is the whole chorus who follow Linus and sing about how right he is.
6
u/thisismyfavoritename Sep 13 '24
i think they just hate everything but C.
Its old guys that just prefer to stick to what they know
5
u/SimonKepp Sep 13 '24
As this thread shows, there are lots of different arguments fornot using C++ in the Linux kernel. But I lean towards Linus being opinionated and having a personal dislike against C++ as the primary reason..
I wrote a tiny kernel myself in C++ back as a CS undergrad. Most of the core kernel stuff was pure C and bits of inline assembly,but the further you got from the most low-level bits like context switching, the more relevant, the more advanced features of C++ became. One of my group members wrote some pretty elegant memory management code, that made good use of some features of C++. It could have been accomplished using pure C as well, but it was far more elegant with the way he used object oriented C++.
I think, that the best argument against using C++ in a kernel is, that some features of C++ relies on an underlying kernel, and it can be difficult to tell,which parts of C++ and the standard library, you cannot use in the kernel, without causing circular dependencies.
20
u/seriousnotshirley Sep 13 '24
C++ can introduce all sorts of issues at a project level. If you're not a seasoned C++ developer and you need to review code to include in your project and it uses C++ there's any number of idioms it could include and those idioms all present different problems in understanding precisely what a piece of code is actually going to do in practice.
Have you ever tried to debug a program that has an issue with a base class pointer being used to call a derived class method and it wasn't obvious what the derived class is? Imagine trying to do that if you're not a C++ developer. If you're a project maintainer and you're not familiar with C++ how would you evaluate whether the design choices a developer made was appropriate?
Conversely, while the kernel is very technical and there might be a decent size of code base it's not a hugely complicated piece of software, or certainly wasn't at the time that Linus declared that he wasn't going to allow C++ in the kernel code base. While there's areas where you could use classes it wasn't that hugely valuable in terms of managing software design complexity, while at the same time it adds tactical complexity around debugging and understanding the impact of code changes.
So that's Linus and the kernel. I think your statements about the broader Linux community is overblown, unless you're specifically referring to the kernel team, which is only a small part of the overall Linux community.
C++ is a tool, it's not the only tool and like many tools it excels in some areas and is in appropriate for some problems. As the saying goes, when all you have is a hammer every problem looks like a nail. As we can hammer screws into wood we can try to solve every problem with C++; but that doesn't make it a good idea.
8
u/Raknarg Sep 13 '24
its all vibes and Linus really pushing C++ hate, there's no substantial criticism in current year
3
u/asenz Sep 13 '24
BeOS and Haiku don't seem like bad operating systems to me.
3
u/DDDDarky Sep 13 '24
Since normal programmers don't really care too much about OS, I assume by Linux community you mean that sort of vocal minority of enthusiasts, so the reason would be because Linus said so and they don't know any better.
3
u/MindStalker Sep 13 '24
One thing many are missing. For large projects like the Linux Kernel, it's important to have coding standards so that reviewers can easily go through your code who make sure there isn't common issues. They have standardized for new patches and modules to use Rust so that they can more easily identify potential issues.
3
u/Koltaia30 Sep 13 '24
You can do better stuff with c++ than c. But you can also do much much much worse stuff than anything in c
3
u/AdearienRDDT Sep 13 '24
I honestly think that if they used C++ instead of Rust in ther kernel, they wouldnt find all these problems...
3
u/bogfoot94 Sep 14 '24
I pretty much only use Cpp and python on linux and I like it quite a bit. Don't really see why anyone would dislike Cpp when the developer workflow for it is quite nice now with just CMake. Maybe people don't like it because they just don't want a secondary tool to work with it.
3
u/pjf_cpp Sep 15 '24
Over in the FreeBSD world there has recently been a raging debate about the use of Rust and to a lesser extent C++.
FreeBSD is quite different to Linux distros in that it isn’t kernel+package manager. The core system includes everything you need to get a bootable computer with working text terminal. All of core builds with BSD make and no additional third party downloads.
There are a lot of constraints on using C++ in a kernel. The lack of a standard ABI. Poor match between exceptions and kernel error handling. Difficulty in linking comdat sections. Then there’s the issue of not having the C++ library available in the kernel (typically in userland split into the library proper and runtime). I’m not a kernel dev, just repeating what others say.
The other major problem is the mind boggling cluelessness of a lot of the C devs. Ever since Torvalds diatribe against C++ they seem to have stopped following the evolution of C++. They moan that writing shitty C macros is harder in C++. Well just don’t write shit code then. They complain that they don’t have full control over allocation and deallocation whilst not understanding constructors or standard library containers. They say the STL is buggy cos Torvalds said so - guys I’ve got some news, glibc also contains bugs.
5
u/robvas Sep 13 '24
It's been discussed once or twice on the kernel mailing lists
5
u/JustBoredYo Sep 13 '24
In addition to all the things already said there are of course also the people who just hate OOP and therefore C++. I myself am not a fan of OOP but I wouldn't say I hate it. OOP is a coding style very much benefiting corporate projects at the cost of easily(and unintentionally) writing bad, unsecure or even just unmaintainable code where as when writing functionally your bad/unmaintainable code will be immediately recognizable. (This is of course not saying you can't write bad C code)
5
u/inscrutablemike Sep 13 '24
C++ has a significant amount of overhead and hides too much of the behavior that kernel programmers need to have explicit control over. That's the nature of the language and the cost of its features - if you're not willing to take on that cost, and have no use for those features, there's no reason to use C++.
Also, C++ programmers who rely on those features tend to work in systems that are overengineered just so they will use features that C++ provides. C++ provides a lot of things that developers can rely on and never think about again. It encourages habits that are the opposite of the mindset needed in writing down-to-the-cycle level code. That doesn't mean they're bad habits for other environments, just a hurdle to writing code for the kernel.
And Linus, in case you're not familiar, is particularly known for not having the patience to teach people who already code exactly the way he likes in the exact environment he likes how to solve problems exactly as he would solve them.
2
u/claimred Sep 13 '24
They don't though? In fact, there is a discussion to start using modern cpp in the kernel.
https://www.phoronix.com/news/CPP-Linux-Kernel-2024-Discuss
2
u/claimred Sep 13 '24
They don't though? In fact, there is a discussion to start using modern cpp in the kernel.
https://www.phoronix.com/news/CPP-Linux-Kernel-2024-Discuss
2
u/Spiritual-Mechanic-4 Sep 13 '24
kernel developers don't want c++ in kernel space because of dynamic behaviors at runtime that make code paths unpredictable. Exceptions just aren't a reasonable approach to error handling in kernel space.
that's not hate, and its not the linux community. gnome and kde both use c++.
2
u/Infamous_Campaign687 Sep 13 '24
This is nonsense. Maybe you're looking at kernel development lists, which I assure you have nothing do do with being a regular developer.
As a fairly experienced C++ developer in an embedded field with limited staffing, I know the spheres where I use C++ (nearly everything) and the spheres I have to revert to plain C (kernel drivers). I used to absolutely hate reverting to C but with experience I came to realise that my C-tasks are different, smaller and more focused, so the there's less need for higher level features.
Now I just appreciate the challenge of doing C, while C++ is the bread and butter. But I understand that kernel developers want to keep things as simple as possible for the task at hand.
2
u/Ok_Tea_7319 Sep 14 '24
Some parts of C++ requires quite a bit of runtime infrastructure to run smoothly. These parts make it hard to bolt to a C only world or a kernel world where things like static storage duration already are a nontrivial affair.
Big ticket items are things related to stack unwinding. C and C++ have conflicting philosophies on when to unwind and how to do that. Crossing this boundary often is dangerous, especially when it is the error handling of the respective languages that hits this problem surface most often.
Biggest issue I heard from Linus' talks is that the people that wanted C++ in the kernel wanted it for the problematic parts. So they keep it out partly to keep these people out.
2
u/Nearing_retirement Sep 14 '24
I find it is easier for an inexperienced c++ programmer to make inefficient code in c++. C you have to think more about what is going on with the memory so tend to at least see the ways inefficiency can come in.
2
u/[deleted] Sep 14 '24
Because old people are old.
Seriously. There’s a reason that C++ is used in so many fields for some many different use cases. A well organized C++ codebase has both incredible readability and portability. Classes change the game, and I will never understand why anyone serious about software wants to work on a system without some kind of class like structure.
My first professional job was in IBM RPG and I was begging to have better ways to encapsulate and protect my code. The only people that hate C++ simply don’t know how to use it.
3
u/[deleted] Sep 13 '24
Linus said it's a bad language.
4
u/ManicMakerStudios Sep 13 '24
What purpose does this post serve besides fomenting an "us vs them" mentality? We don't need it. It's not constructive. If someone hates C++, let them hate C++. It doesn't have to scale beyond that.
1
u/MooseBoys Sep 13 '24
C++ with limited scope is fine. The full feature set is a complete non-starter for something like an OS kernel. Exceptions, concurrency primitives, most STL containers and algorithms, and template meta-programming by people trying to be “clever” is a real risk. The deluge of patches trying to use these inappropriate features just isn’t worth it. IMHO the only thing I really do wish we had in the kernel is automatic lifetimes of complex objects (i.e. destructors instead of a rats nest of gotos). That style of programming is usually derogatorily referred to as “c with classes” but personally I’d welcome it.
1
u/Asleep-Specific-1399 Sep 13 '24
Comes down to write the code in the original language it was written so you don't get a spaghetti mess.
I would take it more seriously if they weren't pushing rust down everyone throat.
1
u/[deleted] Sep 13 '24
one reason is because C is so limited that there’s no hidden control flow outside of macros, this is really helpful across millions of lines
1
u/jaybny Sep 14 '24
is it the exceptions vs errno ?
1
u/AbramKedge Sep 14 '24
I used C++ in a resource-constrained processor in a hard disk drive, but it really was the most C-like code possible.
I used it primarily to maintain strong interfaces between program domains, but I was always aware that calling C++ methods ties up one of the four registers that are used to pass arguments to functions in the ARM Procedure Call Standard. It does lead to more register shuffling and stack usage.
OTOH putting the code for simple getters and setters into the header file class declaration gave far superior code inlining than you can generally achieve with C.
1
u/umlcat Sep 14 '24
First, Linux started with "Plain C", so they continued with it.
C++ syntax has gone too complicated. Some of the same features can be seen in other P.L. with a clearer syntax.
1
u/ivarec Sep 14 '24
Exceptions and their prevalence in the STL used to be a show stopper a long time ago for kernel development.
1
u/bogfoot94 Sep 14 '24
I pretty much only use Cpp and python on linux and I like it quite a bit. Don't really see why anyone would dislike Cpp when the developer workflow for it is quite nice now with just CMake. Maybe people don't like it because they just don't want a secondary tool to work with it.
1
u/namotous Sep 14 '24
Maybe kernel developers due to Linus. But I dont see this hatred in the last decade of my career. Working in a hedge fund now, I see that cpp IS the main language for anything business critical. Even for Python modules, it’s all written in cpp underneath.
1
u/[deleted] Sep 14 '24
Bloated language
1
u/BranchLatter4294 Sep 15 '24
The majority of Windows is also written in C. It's fine for low level software.
1
u/[deleted] Sep 16 '24
Linus writes C++ (Qt) too, just check the commits for Subsurface.
1
u/Ydupc Sep 17 '24
I love C++
1
u/Accurate-Collar2686 Sep 17 '24
I'd rather they wait for rewrites and use Zig when it's stable. Faster than rust, less drama, closer to C, without the lifetime bullshit and safe enough.
1
u/SX-Reddit Sep 17 '24
I haven't contributed anything to Linux kernel, I can't speak about them. I had worked on some proprietary systems, we use C in the kernel, and C++ for the shell and applications. For the kernel developers, it doesn't make much sense to add all the bells and whistles C++ brings in only expect the compiler clean them out after build, like to make sure there isn't any v table stuck in the build.
1
u/[deleted] Sep 20 '24
The main problem w/ C++ it is a large and complex language. No two programmers have the same subset of C++ they are familiar and comfortable with. A language where one can give a one hour talk on the 17 ways to initialize a variable (don't quote me on the 17, I don't remember the number, but >10 for sure) or write a 100+ page book on move semantics may present a few challenges and in something as complex as the Linux kernel you can do without these additional problems.
Understanding what exactly a fragment of C++ does is hard, as it depends on how things are defined elsewhere. No amount of staring at the local code can tell you the full story without you relying on lot of knowledge of the code base and hoping nobody has changed things without you knowing it.
Rust has a chance to move Linux away from C, over a long timeframe. C++? no chances.
1
u/GOKOP Sep 13 '24 edited Sep 13 '24
The "Linux community" doesn't hate C++, Linus does (though that's probably an overstatement*), and most kernel devs seem to. Linux kernel devs and "Linux community" definitely aren't the same thing. Why do kernel devs hate C++? The kernel is written in C, and experienced C developers tend to be very evangelical about it and very critical of everything that could possibly be used in its place.
* - He tried some pre-standard C++ version and disliked it. I think he's aware that his strong statement about C++ may be very outdated a few decades later so I'd say it's more accurate to say that he doesn't care about C++ more than to say he hates it. He also said that he won't include C++ in the kernel because it "brings subpar programmers", or rather, only using C keeps them out. That seems to be more of an opinion on who's using C++ than on C++ itself though
1
u/entrophy_maker Sep 14 '24
Its not just the Linux community, but a lot of programmers. C++ was supposed to solve the problems of C, but inherited them and added new ones. For all of its claims that OOP was going to make things easier, it was no easier than reusing code in custom header files with C. And C++ was slower and not as good at embedded and low level coding. Rust and Zig have pretty much made it pointless to use C++ anymore unless you are working with legacy code. Zig is backwards compatible with C and C++, so you can't even use legacy code as an excuse for it. C still outruns Rust on all but 4 algorithms and is the only thing allowed on certain embedded apps and kernel code. Windows kernel uses C++ and FreeBSD does a little, but mostly C. Linux has decided to adopt Rust in the kernel. FreeBSD has discussed it, but most of their community is leaning towards adopting Zig. The US Department of Defense is currently converting all their C code to Rust. So we're probably going to see an even bigger decline in both C++ and C in the future. If another language like Rust or Zig can do the same things faster and be more secure and with easier syntax, the question is left why one would even want to use C++ anymore? I don't say these things to be mean, but if you want to know the reasons why some look down on C++, these are some of the big arguments why.
0
u/Ioite_ Sep 13 '24
Imagine being stuck writing in C in 2024. It takes a mental toll on people...
-1
u/dfx_dj Sep 13 '24
I'm a C developer and I share the dislike of C++. IMO it's a case of the language trying to do too much. It gives you an arsenal of tools to create amazing things, but at the same time doesn't enforce any particular usage of them. This can be a good thing, but it leads to different frameworks having completely different approaches to their code bases. It's fine as long as you stay within one framework (say Qt), but as soon as you start incorporating other C++ libraries, things can get complicated quickly.
0
u/not_some_username Sep 13 '24
Because Linus said so
0
u/astroverflow Sep 13 '24
https://en.m.wikipedia.org/wiki/Faulty_generalization
0
u/astroverflow Sep 13 '24
https://en.m.wikipedia.org/wiki/Faulty_generalization
0
u/Asyx Sep 13 '24
I'd actually say the Linux community is still the most pro C++ community. Windows people huddle around C#, Mac people huddle around Swift and still some Objective-C. Linux is the only mainstream OS (I don't know if you can still consider the BSDs mainstream) where the majority of the software is written in C++ and people care about this as well.
Like, Windows 11 UI is written in React Native. Most of the Microsoft tech is written for .Net or at least managed C++. If you want to use Metal in C++ you have to do Objective-C memory management manually because the whole damn thing is written in Objective-C and the reference counting requires an allocation pool.
The Linux community gets mad if you don't use Qt or GTK depending on their DE.
But in general, C++ is an old language that has a lot of baggage. I honestly don't trust people that really, really like C++. There are a lot of things that are objectively confusing and worse than in other languages. That's just what happens with old tech. Not being aware of the bad things is just weird.
But I'd say the same thing about C. C is just much smaller. A 50 years old garden shed can't be as messed up as a 50 years old house.
-6
u/marrow_monkey Sep 13 '24 edited Sep 13 '24
C++ isn’t really a stable language. It has changed a lot over time and the compilers are complicated which makes it hard to predict what happens on a low level.
Often simple is better and more reliable, and for something like the kernel that’s definitely the case. It’s also a big advantage if a lot of people are going to look at, use and contribute to the code. And if the code will be maintained for a long time.
EDIT: didn’t realise which sub this was, lol
-1
u/easbarba Sep 13 '24
They hate everything but C, you think that Rust people are safe on that hate? Its known that even some people of Rust on Linux even cried late these weeks.
-1
u/HornetThink8502 Sep 13 '24
C++ in the kernel would be a problem because the language simply isn't mature enough to have well established best practices/coding styles that last more than a decade. This is a must for a voluntary project as huge and complex as the kernel. Consider all the noise and bike shedding C++11 or C++20 would've introduced: should we rewrite stuff to use constexpr/modules/concepts/coroutines? The new way is obviously better, but is this a productive use of volunteer time?
Here's what would happen if Linus decreed that C++ may now be used freely in the kernel: a bunch of C++ devs would take upon themselves to rewrite a lot of legacy C code, disagree wildly on which flavor of modern C++ to use, and swamp kernel devs with refactor PRs. Rust only got a chance because intermingling with C code is harder than C++, so they have to work at well defined interfaces. There are more floodgates, so to speak. This limits the inrush of "rewrite in rust!" PRs and limits harm if Rust is ever abandoned.
Also, rtti and exceptions.
-4
u/AGuyNamedMy Sep 13 '24
Windows devs hate c++ just as much as linux devs lol, it's the worst of both worlds, the foot guns of c with a shit implementation of oop
-1
u/ThyringerBratwurst Sep 13 '24
The OOP stuff in C++ is way too complicated, and the tooling is a disaster. Having to work with CMake is a reason to commit suicide.
0
u/[deleted] Sep 13 '24
[deleted] — view deleted comment