News for hackers. Stuff that matters.
Re:Best aspects of C by jcelerier on Tuesday November 17, 2020 @10:53AM (#25125179)
> * There's a standard ABI

there's one ? the "C" ABI is just the ABI of whatever platform it's running on, which may or may not have funky behaviour that vendor-provided compilers kindly hide for you - functions being prepended with '_' on macOS, the two-dozen calling conventions on windows with i386, sysv and itanium ABIs...

Do you think you can tell what's the ABI of

    struct foo some_function(struct bar);
?

will bar be passed in a register, on the stack ? who knows, that depends on your platform, your compiler, etc etc. Things going cleanly on the stack is just a convenient lie that your first year comp. sci teachers tell you because it's too early to talk about how the real world works yet.

Re:Best aspects of C by pavlov on Tuesday November 17, 2020 @10:59AM (#25125260)
> the "C" ABI is just the ABI of whatever platform it's running on

But typically the C ABI is the only stable ABI those platforms provide. That's a huge benefit.

Re:Best aspects of C by brandmeyer on Tuesday November 17, 2020 @11:54AM (#25126009)
Re:Best aspects of C by setpatchaddress on Tuesday November 17, 2020 @01:53PM (#25127952)
"platform", not "ISA". Windows on Intel doesn't use the same ABI as linux.
Re:Best aspects of C by StillBored on Tuesday November 17, 2020 @02:37PM (#25128583)
Windows doesn't even have a single calling convention...

https://docs.microsoft.com/en-us/cpp/cpp/argument-passing-an...

Re:Best aspects of C by will4274 on Tuesday November 17, 2020 @08:24PM (#25132109)
You mean 32 bit Windows. 64 bit Windows only has one calling convention.
Re:Best aspects of C by jcelerier on Tuesday November 17, 2020 @04:18PM (#25129986)
and that is without counting Cygwin which IIRC uses another calling convention
Re:Best aspects of C by flohofwoe on Tuesday November 17, 2020 @11:15AM (#25125471)
The leading '_' before symbols isn't just a macOS thing, AFAIK this is the only "name mangling" that C does and from what I've seen so far it's the same across platforms and compilers.

As far as the ABI goes: the important thing is that there is a standard ABI on a specific platform that all compilers on that platform agree on. Sounds kinda obvious, but it's not common in other languages.

Re:Best aspects of C by jcelerier on Tuesday November 17, 2020 @11:23AM (#25125559)
> The leading '_' before symbols isn't just a macOS thing

it definitely is - see e.g. https://github.com/llvm-mirror/lld/blob/master/lib/ReaderWri...

Re:Best aspects of C by flohofwoe on Tuesday November 17, 2020 @11:27AM (#25125621)
Oh ok, that must be an extra leading underscore then, haven't stumbled over this yet though.
Re:Best aspects of C by jcelerier on Tuesday November 17, 2020 @11:45AM (#25125871)
the only cases you stumble over it is if you're trying to do linking / symbol loading yourself manually (e.g. in my case it was because I was looking into JIT compilation mechanisms)
Re:Best aspects of C by int_19h on Thursday November 19, 2020 @04:07PM (#25154363)
ABIs have always been platform-specific in that sense. C ABI is beneficial in that on any specific platform, it's interoperable - which is good enough, because native code has to be compiled for a specific platform, anyway. In your specific example, I don't really care how "struct foo" is passed, so long as all shared libraries compiled for that platform agree on how it's done. And we have that on C level today on all major platforms.

Sure, there are compiler switches and language extensions that can break the ABI if you use them. But, well, you don't have to use them (at the interop boundary), and neither do your API clients.

Re:Best aspects of C by sumtechguy on Tuesday November 17, 2020 @02:22PM (#25128378)
C was one of those write once compile anywhere bits they used to tell us. Then when we actually tried it we found every CRT was different on all of the platforms. Even something as ubiquitous as printf I think I have encountered at least 8 different versions of it. At least from compiler chain to compiler chain they are usually similar in calling conventions (but not always). But try mixing a msvcrt with a glib one and you are in for some fun...

What is the nit of it is it almost works. You have a good shot at getting it to compile in a short amount of time. The rest of the work will be lots of time in ye old debugger and going over the docs for your platform. The fun part is you will find bugs that were there already, or are they just part of the platform, or were you using it wrong?

Re:Best aspects of C by jesuscyborg on Tuesday November 17, 2020 @03:05PM (#25128969)
If you want a CRT that's compile-once run-anywhere for x86 then try cosmopolitan with actually portable executable. https://justine.storage.googleapis.com/ape.html It even supports fork() on windows as of a few days ago: https://github.com/jart/cosmopolitan/commit/db33973e0aae7ffc...
Re:Best aspects of C by sumtechguy on Wednesday November 18, 2020 @04:23PM (#25142622)
That is 9 :)

In effect it is yet another CRT and the idea is sound. But many times what I found was you may have one that works on say linux and windows and bsd. All the same 'code' but you dig under the covers a bit and it is a maze of ifdefs so each platform has its own quirks. For example threading between fork and createthread is on the surface not too different and you can wrap createthread with it (several libs did). But you dig into it a bit and you find portions that just do not map at all between the systems (usually with IPC and locks). At best they do not compile, slightly worse they return error codes, at worst they act like they work.

A real good example of what I am talking about is the pthread library. It works up to a point but it is a very linux/bsd orientated library. There are some gaps in there from windows that just do not map and the other way around. What is worse is the docs on some of these do not talk about cross platform issues. Luckily you can see the source code of most of them and can tell what is going on. Annoying but one of the things I learned moving code between platforms is that each one has its own way of doing things. You can try to work against it or sit down and unwind what is going on, which takes time. I have even seen this sort of issue in python and java. Where you get down to some low level thing and it just is different on different platforms.

Re:Best aspects of C by int_19h on Thursday November 19, 2020 @04:10PM (#25154391)
C is write once, compile anywhere... so long as you restrict yourself to standard C, including the library. Different implementations have different levels of standard compliance, but you are still able to say, "this will do X on any conformant hosted C89 implementation".

The moment you start doing things like threads or shared libraries, yeah, it all breaks down very quickly. But that isn't standard C.

Re:Best aspects of C by sumtechguy on Monday November 23, 2020 @11:42AM (#25188603)
Even things like printf/sprintf act differently from library to library. trust me, read the docs on your lib. Most of the time they are the same but not always. It is one of those things that looks like it works but in practice there are a ton of gotchas. Most of the more mature libraries are getting better but there are some edge cases out there.
Re:Best aspects of C by int_19h on Monday November 23, 2020 @06:25PM (#25193155)
They are different mostly because of different standards that libraries support - C90 has a baseline list of %-specifiers, but C99 added a bunch more, and then I think POSIX also has some now? Plus extensions.

But I can't think of any implementation that doesn't conform to C90 in that regard. So long as you don't venture into implementation defined / UB category...