r/cpp_questions • • 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

192 comments sorted by

View all comments

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.

97

u/d1722825 Sep 13 '24

To be fair, in the linux kernel many C++ features (eg. inheritance, RAII, virtual function calls) have been implemented in a worse or uglier way just to remain in C.

C++ was many issues, but it changed a lot since Linus's decision. And bad programmers can write bad code in any language.

2

u/tyler1128 Sep 14 '24

Yeah, you can always manually implement anything, the big difference is that you have full control. Just throw a function pointer into a struct in C and you have basically the same thing as a class, just less conveniently. You can write your own vtable which is how inheritence works in C++. Just look at GDK, they made a full object oriented system with features beyond what C++ supports in C. It also is horrible to work with through C.

→ More replies (3)

18

u/clusty1 Sep 13 '24

Recently I wrote again some C ( ported c++ ) and damn, the mem management is a pain: allocation of temp scratch buffers forces a ton of boilerplate.

In no way is such code faster, just uglier and more error prone.

2

u/SlothWithHumanHands Sep 16 '24

and the moment some memory needs to stick around, or has non-trivial ownership, slow drag into heap abyss

1

u/jediwizard7 Sep 17 '24

I remember once doing something simple like I wanted to split a path with dirname and basename, and with the posix c APIs you need to strdup the string before calling the functions because they modify the string in place (so two strdups to call both), and then you need to strdup the results as well or keep around the input copies since the result is either a substring of the input or a static pointer.

15

u/[deleted] Sep 13 '24

[removed] — view removed comment

16

u/tyler1128 Sep 13 '24

It's probably a lot more accessible to do these days, but you'd still need a stripped down version. Windows allows parts of C++ to be used in kernel space, but including a large standard library will affect boot time and binary size. I doubt Zircon uses the full STL, though I'm not familiar with the source code.

EDIT: To add, it's why Rust has a stripped down version of it's standard library that is supported by the language team. Called the rust core library in their wording, which is separate from the rust standard library.

1

u/smirkjuice Sep 14 '24

Windows allows parts of C++ to be used in kernel space, but including a large standard library will affect boot time and binary size.

They would have to write their own standard library though, and they'd do something to make it not affect anything, C++ still has freestanding headers and shit

7

u/Nychtelios Sep 14 '24

I work on baremetal firmwares in C++23, your statement on the STL is wrong just because you cannot include the whole STL, it generates code on the final executable almost only when used.

And, anyway, C++ abstractions are not properly abstractions, they are almost only syntactic sugar and zero-cost abstractions. Torvalds statements about C++ are not only strongly opinionated, but they hide a strong ignorance on the theme (he only refers to 30 years ago C++) and a (imo) ridiculous classism against C++ developers.

The last point I have to report: Linux code is not properly C. It makes heavy use of GNU extensions, and those extensions are in great part already covered in the C++ standard. They even had to implement tons of custom vtables, but hey, it is wrong to use C++ :)

2

u/tyler1128 Sep 14 '24

And, anyway, C++ abstractions are not properly abstractions, they are almost only syntactic sugar and zero-cost abstractions.

That's what abstractions generally are in native languages. Not always zero cost nor are C++'s all, but they often at least can be next to zero-cost. When we talk about virtual in C++, it's usually not zero-cost, it requires a relative address function call which takes a lot longer for any ISA to perform. There's a reason C++ doesn't virtualize every class or call.

If you want specifics, a virtual call requires two things - a pointer table added to the class, and then the instructions that looks up the pointer from memory, and do a relative jump to that place. A non-virtual function has the ability to either be inlined, meaning recreated in place where it is used, or called by a known address without memory access. Memory access is slow, and even if the address is cached or in a register, indirect jumping ruins a lot of optimizations.

→ More replies (2)

8

u/[deleted] Sep 13 '24

[removed] — view removed comment

5

u/tyler1128 Sep 13 '24

That is part of it, and the lack of a large standard library. There's a reason Rust has a core library and standard library, binary size in things like the initial image you load after booting still do matter.

1

u/angelicosphosphoros Oct 07 '24

Rust library split into 3 part not to minimise its size (it is a job for compiler and linker to remove unused code anyway) but to be available of different levels of host features. std requires a working operating system (e.g. filesystems, mutexes, threads, ...), alloc requires only availability of a heap memory allocation (so it can be used as long you have nothing but even simplest allocator), core is for cases when you don't have anything, even a heap (e.g. if you are implementating a bootloader).

3

u/ksobby Sep 14 '24

I also always thought that with C, everyone was more of a waterfall style programmer when they cut their teeth or learning with Fortran, assembly, etc. OOP wasn’t a real, widespread thing quite yet. Once everything started to move to abstraction and C++ was being positioned as the next big thing, the divide became greater. At least that’s my vague recollection of coding in the late 80s-mid90s … and yeah, I have an irrational hatred of Java and yet love C#. We’re just weird all around when it comes to loyalty and preferences of any coding language regardless of age.

5

u/davidc538 Sep 13 '24

I think linux is the only major kernel without c++

7

u/rysto32 Sep 13 '24

None of the BSDs have C++ in their kernel. 

1

u/davidc538 Sep 17 '24

That’s interesting, always thought they did. I guess just XNU and MS kernel use c++ then

1

u/MajorMalfunction44 Sep 17 '24

There's also exceptions to deal with. It's quite a complicated mechanism. I prefer C, just due to ABI complexity. EDIT: gamedev context - I serialize in-memory structures to disk, and use that in PAK files, etc.

1

u/tyler1128 Sep 17 '24

You can disable exceptions in C++, but yeah, I agree they aren't a great error propagation mechanism. It's also why std::expected is being added, mimicking the Rust control flow mechanism for error handling, which I also think is better than C's.

ABI issues aren't a huge deal on x86 outside of windows, where they are a huge deal if you want to link something compiled with even a slight update to msvc. Linux and OSX use the Itanium ABI. However, the C ABI is and probably always will be king anything using native symbols in object files. It's effectively the minimal ABI to call a function and will always remain the cross-language ABI for the forseeable future, and for good reason. C absolutely still has a reason to exist, but C++ does too, in my opinion.

→ More replies (3)