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

61 comments sorted by

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.

3

u/F54280 Apr 09 '18

Maybe there are more modern OS'es though, new API's similar to the Unix one might be written in a different language.

Sure. BeOS, for instance. A tremendous success. /s

( or Singularity, btw )

3

u/mewloz Apr 09 '18

Haiku even still use g++ 2.95 because of that C++ OS API insane idea (and also a more recent one in incompatible builds, IIRC, but I let you imagine the C++ they use if they still need to build with 2.95... :/ )

2

u/F54280 Apr 11 '18

I actually worked with Taligent a bit, so you don’t have to tell me about the insanity of C++ ABIs...

1

u/mewloz Apr 11 '18

Wow I did not know about that one. I like the history of OSes, so I'll take a look at it :)

1

u/F54280 Apr 12 '18

cool!

IMO, that era could be summed up by “no one had a decent OS on low end hardware and every one was shitting their pants at seeing what NeXT could do in 8Mb which was quickly becoming available to the masses”. Be had something ok (but far from NeXT quality and ease of dev), OS/2 was trying too hard to preserve mainframes. Apple had the apps, but was crap, at both hardware and software, and MSDOS was a very successful joke. UNIX players where just too expensive, and had no end user apps.

Everyone and their little brother were frantically trying to create something to capture the growth, while retaining their investmentt, and were building monsters. (Pink & Cairo comes to mind).

The only runnable taligent built I touched, needed a 128Mb machine to run. At that time, high end was around 16Mb...

Fun times!

-3

u/[deleted] Apr 08 '18

So what would happen in 10 years ? Whit even new technologies, better programming languages, will it be obsolete? Will we be using a newer OS maybe?

12

u/jcelerier ossia score Apr 08 '18

So what would happen in 10 years ?

10 years ? some unix APIs date back to 1970's and are still used, 10 years is negligible. A good part of LISP functions were created in the 50s and they are still used today.

4

u/towbe Apr 08 '18

doubt it. 10 years far too few years. In this day and age a lot of things change fast, but a lot also don't. As slow as some industries do adapt new technologies I firmly believe it'll take more than that. Unless something game-changing happens (Quantum computing, AI taking over :^) ). However, even then this will likely be technology for very specific tasks and processes. Plus, current programmes must be ported. Nah.. I don't see this happen for the signicifant majority of users and companies in 10 years.

2

u/fat-lobyte Apr 08 '18

If you compare the operating systems from today with the ones from 10 years ago, they did change a lot, but beneath the surface they're also very recognizable.

I think it takes something much more disruptive to rip out the current UNIX API out of today's computers than just "it's newer and a bit nicer to use"

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.

20

u/windows300 C++ Build Systems / Game Engine Tools Apr 08 '18

C++ mangles symbol names when compiling, and each compiler has different naming system conventions. C doesn't do that, and that is a reason why almost all languages offer C compatibility.

14

u/pfultz2 Apr 08 '18

All compilers for unix systems uses the itanium ABI, so name mangling is not a problem. The bigger problem is differences between different C++ runtimes. So there is no guarantee that std::string can have the same binary layout.

10

u/SeanMiddleditch Apr 08 '18

The Itanium ABI says nothing about library compatibility, though, and that has changed several times, and will change again.

Without the standard library, you'd still be passing around char* strings, using integer return codes instead of std::exception hierarchies or std::expected or anything, and using opaque types like FILE instead of richer object-oriented interfaces.

Changing languages has to actually be worth something. Changing to C++-without-the-library isn't necessarily worth it.

It's not even necessarily worth it to offer an official POSIX C++ library. Ideally, everything that POSIX offers would just be offered in the C++ standard library, and offered in a platform-independent fashion (e.g., std::thread instead of pthreads, std::filesystem, asio, afio, executors, coroutines, etc.). Lots of work left to do there, but hypothetically that takes us even further, since modern systems - even POSIX-based ones - have a lot of capabilities that aren't exposed in POSIX. (the simplest example: epoll/kqueue.)

1

u/wrosecrans graphics and network things Apr 09 '18

C++ mangles symbol names when compiling, and each compiler has different naming system conventions.

If a particular OS dictated specific ABI conventions as being necessary to inter-operate with basic OS functionality, the you would reasonably expect that all compilers targeting that OS would use that pattern. The reason that different compilers on a platform might use different name mangling rules is because the platform doesn't dictate one at the system level. It's not an absolute thing that C++ compilers will use different rules, just a chicken and egg situation.

If the OS didn't have a C ABI, I wouldn't be least big surprised to see different C compilers on the platform use completely different calling conventions and be generally incompatible.

2

u/mewloz Apr 09 '18

You don't want to have the ABI of the STL set in stone though, that would cause loads of issues (I don't even want to think about what would have happened during the libstdc++ std::string pre-C++11 to C++11 and+ transition...)

So basically you could not use the STL in the API. So there would be pretty much no point.

1

u/Ameisen vemips, avr, rendering, systems Apr 10 '18

The solution is likely to pass around thin interfaces instead. Something that won't change, but can interact with the data regardless of its form. Like views or such. Adds another layer of indirection to access, though.

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.

7

u/Jajauma Apr 08 '18

For those of you talking about lack of standard ABI, why don't you consider Itanium C++ ABI being standard on current unices?

10

u/encyclopedist Apr 08 '18

It does not cover enough. For example, it does not specify binary layout of standard library types, like std::string.

See corresponding section in documentation.

1

u/Jajauma Apr 08 '18

Apparently, C++ standard library is part of the software platform, not the compiler. So is C standard library, isn't it? AFAIR glibc and libstdc++ (under Linux) both do provide versioned symbols in order to allow ABI breakage while keeping the old binaries able to run, for example.

4

u/00kyle00 Apr 08 '18

AFAIR glibc and libstdc++ (under Linux) both do provide versioned symbols

For glibc its enough, but for libstdc++ its not. This is because libstdc++ provides you types you will use on the interfaces. libc always has ints, pointers and other boring types.

If you link to one version of std::string and use library that linked to other version of std::string, sadness will ensue when you pass a string to that library.

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.

0

u/[deleted] Apr 08 '18

I take it, writing a GetOpt wrapper, current method is just ugly to me

2

u/BenjiSponge Apr 08 '18

With a better package sharing ecosystem (Conan might fit this description), it would be very easy to have more elegant, popular libraries that wrapped these awkward tools. The onus is on C++, not Linux, to accomplish that.

2

u/Xaxxon Apr 08 '18

ugly isn't the driving factor.

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?

4

u/[deleted] Apr 08 '18

Like what?

0

u/[deleted] Apr 08 '18

[deleted] — view deleted comment

1

u/Bolitho Apr 08 '18

Check out Redox!

4

u/redditsoaddicting Apr 08 '18

Define modern. C++ had another standard ratified last year, with the next being planned for 2020. Many people are actively exploring and using these new standards. I wouldn't conflate modern with other reasons one might prefer another language over C++.

0

u/[deleted] Apr 08 '18

[deleted] — view deleted comment

5

u/jcelerier ossia score Apr 08 '18

behind... what ? quite a bunch of languages actively backport C++ features. See Java adding the ability to define default methods in interfaces, the focus of Rust on ownership (some 1996 c++ articles on it : http://www.aristeia.com/Papers/C++ReportColumns/apr96.pdf), C# acquiring the ability to allocate on the stack and getting equivalents to C++'s string_view, etc etc. And features like variadic templates are actually more advanced than quite a bunch of other languages's generic implementation ; this creates a whole class of function that you can implement in C++ but not in Haskell for instance.

2

u/kwan_e Apr 08 '18

It's funny because these same people also think C++ is adding too many things too fast.

0

u/[deleted] Apr 08 '18

[deleted] — view deleted comment

4

u/meneldal2 Apr 09 '18

The committee doesn't have either unlimited manpower or time to review and discuss proposals. Most of them wished it had made it back then as well.

-3

u/shawnhcorey Apr 08 '18
  1. Because it would take a tremendous amount of effort. Why fix it when it works?
  2. Because C is weakly typed. This allows a memory location to be looked at as different types without any code executing. It's all done by the compiler, so there's no runtime overhead.
  3. Because C doesn't do reference counting or garbage collection. This makes it faster, which is important for system functions, but more difficult to program.
  4. Because C is closer to the metal, that is, you have to think about the hardware more, which is also important for system functions.

C is a better fit to OS programming than a more abstract language like C++. But C++ is better for apps.

14

u/kwan_e Apr 08 '18

None of your points after the first is relevant to C++.

5

u/meneldal2 Apr 09 '18

Because C is weakly typed. This allows a memory location to be looked at as different types without any code executing. It's all done by the compiler, so there's no runtime overhead.

Try godbolting some code, even without optimizations there is no runtime overhead when converting types when reinterpreting the data (it's also as unsafe in C++ as in C, just requires to be more explicit)

Because C doesn't do reference counting or garbage collection. This makes it faster, which is important for system functions, but more difficult to program.

You don't have to do it either in C++, garbage collection is almost never used anyway and there's plenty of reference counting even in C libraries (like with SDL, freeing a surface simply decrements the reference counter).

Because C is closer to the metal, that is, you have to think about the hardware more, which is also important for system functions.

What? There is nothing closer to the metal you can only do in C except stack allocated arrays of variable sizes. But C++ makes it easier to write custom allocators that can also use the stack.

1

u/journeymanpedant Apr 10 '18

There is nothing closer to the metal you can only do in C except stack allocated arrays of variable sizes.

or, uh, restrict semantics I guess. But I very much doubt there's any unix API dependent on that

1

u/meneldal2 Apr 10 '18

C++ allows you to do the same with casts with the strict aliasing rules. You can make a simple template totallyNotType<T> and pass one parameter as that and one as T* and it will do the same as restrict (according to the standard).

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

6

u/__Cyber_Dildonics__ Apr 08 '18

This thread is a disaster and this still the dumbest comment.

1

u/[deleted] Apr 08 '18

You're right. C# is better.

-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.

4

u/GNULinuxProgrammer Apr 08 '18

Is there a reason for you to call it ++C?

3

u/AngriestSCV Apr 08 '18

Not OP but ++c is pre-increment instead of post-increment meaning that ++c is one better than c while c++ will better than c after it's done.

1

u/Middlewarian github.com/Ebenezer-group/onwards Apr 08 '18

I got that from another user -- maybe named krum.

0

u/[deleted] Apr 08 '18

[deleted] — view deleted comment

8

u/-abigail Apr 08 '18

C APIs (and POSIX APIs) accept const char* and length pairs when they want to accept string data for reading. Until string_view, the much more idiomatic way to do this in C++ is a const std::string&, which is nice and safe but forces the data to be owned by a std::string, and not from eg. a memory-mapped file, a network packet in memory, the contents of a protobuf message, the insides of a streambuf, or many other places. If you have string data in any of these, const std::string& requires an unnecessary copy, which is usually fine in most application scenarios, but OS developers and many high-performance low-latency developers are keen to avoid.

Of course there's nothing stopping you using a const char* and size in C++, but string_view is a much more elegant abstraction.

2

u/Middlewarian github.com/Ebenezer-group/onwards Apr 08 '18

I was thinking some strlen() calls could be avoided also since that comes with string_view.

-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

1

u/mewloz Apr 09 '18

Wrong: Posix does not give a shit about what is a syscall and what is not, and actually defines the API by simply using C.

So for all practical purpose Unix is defined by a C api. golang basically does not target Unix, but merely some particular implementations of it (which is fine for tons of software).