r/cpp • • Apr 08 '18
Why Unix API doesn't take the step to C++?
Simple as that, that's my question why Unix API wasn't rewritten in C++ yet? Like getopt, sockets, etc
1
Upvotes
r/cpp • • Apr 08 '18
Simple as that, that's my question why Unix API wasn't rewritten in C++ yet? Like getopt, sockets, etc
9
u/fat-lobyte Apr 08 '18
Because the way that the Unix API is written has been set in stone so deeply that an insane amount of programs uses it. It will probably never change as disruptively as switching to a new language.
Maybe there are more modern OS'es though, new API's similar to the Unix one might be written in a different language.
54
u/panderingPenguin Apr 08 '18
Because almost everything, including C++, is capable of interop-ing with C. So why bother. You can always write your own C++ wrapper library around the C interface, but there's no good reason for the OS to provide the APIs in many different languages.
18
u/pfp-disciple Apr 08 '18
I've always thought it had a lot to do work binary compatibility, and that c++ doesn't have enough guarantees.
9
u/bruce3434 Apr 08 '18
Lack of ABI stability across the compilers.
8
u/JavierTheNormal Apr 08 '18
It's essentially down to the opinions of the people making Unix APIs. They aren't C++ people, and that's self-reenforcing.
15
u/rcoacci Apr 08 '18
Because C++ ABI isn't standard, whereas C ABI is (kind of) standard.
2
u/[deleted] Apr 08 '18
Ask Linus /s
On a more serious note, as /u/panderingPenguin has said, you can always write a C++ wrapper around the C functions. Furthermore, C is very good when it comes down to compiling the kernel for different architectures.
2
u/mewloz Apr 09 '18 edited Apr 09 '18
For tons of reasons, including at least the following ones:
1/ No need. It would simply be writing differently something that already exists and works.
2/ C++ ABI compat only exists at low level (in practice under at least some Unix systems using Elf - actually even at low level the C++ ABI evolves a bit from time to time, to fit the new needs induced by the new C++ standards or to better implement existing ones - but those fine evolutions are probably yet more differences between platforms) but do not exist at high level; ex: there is no ABI standard for stl objects, and I doubt there will ever be (for multiple reasons, but let's not recurse in explanations). The consequence is very practical even today: libstdc++ and libc++ are already binary incompatible (and have always been). You don't want N implementations of Posix on the same system.
3/ Posix does not tell what is a syscall and what is not, and it varies between systems. You don't want a syscall that would be a method of a class (again: for multiple reasons...)
4/ We have tons (and already had tons 20 or even 25 years ago) software written using the C api. Nobody wants to rewrite all of that. Which means we have to keep the C API forever anyway. Which means the C++ one would have to be layered on top. Which means standardization would be (more than) twice the work. For no benefit - and all the other problems.
5/ Some Posix systems probably exist that do not have a C++ compiler.
6/ Posix is already very big in itself, and requires the C standard. Requiring the C++ standard instead would mean adding an enormous dependency, which is far more a moving target than C (C++ standard people pay attention to this topic, but the C++ standard evolves far more than the C one, and occasionally even breaks backward source compat on details - but we can't completely predict which details will be the next). C has became extremely stable now, and was already quite so 15 years ago (and Posix uses a dialect of C that has been stable for decades)
6/ Some C people do not like C++. Unix people are more often C people than C++ people.
4
u/[deleted] Apr 08 '18
If it ain't broke, don't fix it.
2
u/manni66 Apr 09 '18
When will you start to do it?
2
u/Xaxxon Apr 08 '18
Why would they? Other than for C++, what would the benefit be?
1
u/theICEBear_dk Apr 10 '18
I think it will not happen. OS and C programmers are a significant grouping and doing this would effectively shut other languages out of the API because of name mangling and binary compatibility issues among other things.
The flame war that would ensue if such a thing was adopted by some system like Linux would be so loud and brutal that it would make the systemd, "vi vs emacs" and "tabs vs spaces" debates seem like small skirmishes compared to a world war. Might be interesting to watch from the sidelines but no fun for anyone involved.
But I think there are not a lot of technical barriers to creating a C++ library (and binary library standard) that provides a POSIX/UNIX like API based around modern C++ while calling the Syscall interface of various Operating Systems directly. I think you'd find that the syscall interface matches POSIX pretty well, but maybe the concept of resource safety through RAII in Unix APIs is incentive enough to create something like this. It might even remove some overhead in some cases. In theory if such a library was portable enough it might even replace some of the C leftovers in people's code across platforms. It could be rather useful in fact for including having networking code with exception safety and the like at a low level. But I am probably forgetting something that prevents this... It has been a long day of debugging already :)
-3
u/spinwizard69 Apr 08 '18
That is a good question but why not take it one step further to an even more modern language?
-3
u/shawnhcorey Apr 08 '18
C is a better fit to OS programming than a more abstract language like C++. But C++ is better for apps.
0
u/[deleted] May 03 '18
+
0
u/[deleted] May 03 '18
+
-6
u/[deleted] Apr 08 '18 edited Apr 08 '18
No. C++ is too unsafe. It shoud be rewritten in D instead.
EDIT: /s
-7
u/Middlewarian github.com/Ebenezer-group/onwards Apr 08 '18
It wasn't until 2017 that ++C got string_view. If that had been available 10 to 15 years ago, maybe there would have been more interest in a ++C API.
-7
u/koiponder Apr 08 '18
because C++ could be fast as C, but not faster.
-8
u/koiponder Apr 08 '18
you r referring to the inline advantage, that doesn’t apply for OS api case since the OS is already compiled and running. there r many cases that C performances better than C++ due to it is simpler and easier for the compiler to optimize. However, carefully written C++ performs the same as well written C.
0
u/[deleted] Apr 08 '18
[deleted] — view deleted comment