Hacker News new | ask | show | jobs
by bestinterest 1123 days ago
Noob question,

What makes languages like Zig, Rust, C and C++ the best fit for cross platform applications over many garbage collected languages? Why is bringing the language runtime a problem?

What does it mean to compile to a C-compatible library?

EDIT:

I decided to ChatGPT my question instead.

https://chat.openai.com/share/9a5f9f7a-0f5d-4cf6-95fc-4e0ec9...

4 comments

ChatGPT's responses start accurate then quickly go off the rails. The section from this point onwards is completely incorrect:

> Say I called a bunch of goroutines when I was in the Add function of the example you gave, would this be a problem?

The Go runtime is initialised once only in c-shared mode for the lifetime of the application - it would make no sense to do it on every function invocation, and be incredibly slow. So the answer to this section and the next one are just largely bogus.

ie. this response

> However, once you call a function via a C or Swift bridge, it becomes a synchronous operation and will block the calling thread until all goroutines have completed execution. Therefore, you would need to effectively manage the synchronization of these goroutines to avoid unnecessary blocking of the calling thread.

And the response to this question:

> You said in 4 the Go runtime may not keep running, does this mean that every invocation of the Add function has to spin up the whole Go runtime every time? Why cant it just stay alive inside the Swift process?

Are completely incorrect.

Pretty much every modern language (Zig, Rust, C, and C++ included) depends on a runtime. The C runtime is privileged because it is already present on all 3 desktop OSes.

It is also a lot smaller than most other runtimes, which makes bundling the C runtime with the program more palatable.

A "C-compatible library" is a library (i.e. a collection of functions) that is callable in the same way that functions written in C are called. Nearly all non-C languages provide a way to call C functions (because, again on all modern desktop OSes, the operating-system interface is written in C).

If everyone wrote OS interfaces in perl, then you would want to compile to a perl-compatible library. If the Lisp machines had won, then you would be compiling to a Common Lisp compatible library.

That is somewhat conflating the calling convention and the runtime - C really doesn't have much that you can call a runtime outside the calling convention, although some libraries (eg pthreads say) have a little runtime which matters in practise for integrations, but these are libraries not parts of the language itself.
malloc and free are part of what I would consider a runtime. As are the file objects opened for standard I/O.

It's a very small runtime, but all the code before main is properly runtime initiation

> The C runtime is privileged because it is already present on all 3 desktop OSes.

Yes, Ubuntu, Debian and Fedora.

On Windows, you don't get a C run time; you ship a MS Visual Studio run-time DLL if you're using Microsoft's tools (and not static linking), or something else with someone else's tools; maybe a CYGWIN1.DLL or whatever.

You get platform libraries like kernel32.dll and user32.dll; but those are not a C run-time. They are easy to call from C, but other than that, they are the OS run time.

Recently Microsoft has made an effort to create a "Universal C Run Time" for Windows; but I think you still have to download and ship that, and there may be reasons for someone to choose a different run-time. (E.g. needing a pretty detailed POSIX implementation.)

I thought msvcrt shipped with windows as early as XP; was I wrong?
Even earlier; but that's a private system library you're not supposed to use.

Raymond Chen explains it: https://devblogs.microsoft.com/oldnewthing/20140411-00/?p=12...

Microsoft's new Universal C Run Time addresses the problem of there not being a C library on Windows that is for public use (every compiler vendor providing their own).

Curious to know from experts in the above to see if ChatGPTs response is valid? Me as someone who knows nothing about either, looking at a nuanced response from ChatGPT, puts me at awe - esp. response to the question: "Say I called a bunch of goroutines when I was in the Add function of the example you gave, would this be a problem?"
As I responded in a sibling comment, this is the point where ChatGPT goes completely off the rails and starts fabricating responses. Temper your awe :)
It's about the libraries usually, ui lib are all made in c/c++ and those "natives" langage have better integration with it ( ffi ).

For example calling C from Go is doable, but it's not encouraged and sometime a bit slow.

On the other hand Go and Java are trully multi platform and it's well supported and usually easier to do than other native langages.