Live data from Hacker News

C-Macs – a pure C macOS application

github.com

81–90 of 138 posts

Re: C-Macs – a pure C macOS application

#81
post #7
post #6

As someone with no experience in native application development, could someone explain to me why this is significant? I have a rough idea, but I would like to understand it properly.

I guess it's normally impossible to write a GUI application for mac os without objective-c libraries doing the talking to the OS, but what do I know.

Beneath Cocoa are Core Foundation and things like Core Graphics for example, which both are C.

Re: C-Macs – a pure C macOS application

#82
post #9

Earlier quoted context omitted.

Does this mean that, theoretically, this could lead to the ability to build MacOS apps in higher languages that interoperate well with C such as Python? I know you can build MacOS apps with Python now, but does this potentially improve the experience?

You can already do this in the traditional way by building an ObjC shim which exposes a C API. The solution shown here just skips ObjC and talks directly to the ObjC runtime (which has a C API but is not as convenient to use as doing the same thing in ObjC or Swift). In a highly simplified way you can think of Objective-C as preprocessor which replaces the ObjC syntax sugar with C function calls into the ObjC runtime…

That’s essentially what this project does. It creates the C code that the ObjC compiler would generate to “implement methods” or “send messages”.

It’s somewhat doable by hand because Objc is a thin lawyer.

Over 15 years ago I did stuff similar to this project to call some Objc code from a C++ app. Most of it was exposed to normal C APIs but one feature only available in AppKit. It was much simpler to do it this way than figure out how to make GCC or Objc like our C++ or any mess with bridging headers.

I think the move to Swift has made that harder in some ways.

But then again I don’t want to write C or C++ these days if I can avoid it.

Re: C-Macs – a pure C macOS application

#83
Seeing some confusion. This is using what are effectively FFI interfaces to the Objective-C runtime. There’s really no good reason to do this in production code that isn’t a language bridge. It’s not as efficient at runtime as writing the functionally equivalent Objective-C, because the ObjC compiler statically allocates class/method data structures, and it’s not as safe, because you’re bypassing ARC.

Re: C-Macs – a pure C macOS application

#84
post #52
post #49

I found this repo while looking for an equivalent to Win32 hello world[0] as a learning exercise during a long flight (with my work MacBook instead of my personal Windows machines). That's something I really like about Windows APIs — I can pick a new programming language I want to play with, as long as there is a way to interface with C I can try to port the Win32 hello world then play around. 0: https://learn.micros…

The WIN32 API is somewhat ugly. I hate how all types are capitalized and their short abbreviations in arguments are sometimes weird. I hate how they have their own version of lots of standard C types that you have to be careful about (they weren't standard at the time, to be fair). But it's quite straightforward and easy to use. Any language that has FFI can call into it and build GUI apps, I've made a windowed Hello…

[deleted]

Re: C-Macs – a pure C macOS application

#85
post #9

Earlier quoted context omitted.

Does this mean that, theoretically, this could lead to the ability to build MacOS apps in higher languages that interoperate well with C such as Python? I know you can build MacOS apps with Python now, but does this potentially improve the experience?

You can already do this in the traditional way by building an ObjC shim which exposes a C API. The solution shown here just skips ObjC and talks directly to the ObjC runtime (which has a C API but is not as convenient to use as doing the same thing in ObjC or Swift). In a highly simplified way you can think of Objective-C as preprocessor which replaces the ObjC syntax sugar with C function calls into the ObjC runtime…

In fact, early objective-c was a preprocessor according to Wikipedia!

Re: C-Macs – a pure C macOS application

#86

Earlier quoted context omitted.

I’d say the license is: “ Use it at your own risk, and don't blame me if anything bad happens. Oh, and if you extend it, make sure there isn't any Objective-C in it!” I’d classify it as Open Source.

It's open source but it is still under copyright. There are notices in the source file: https://github.com/CodaFi/C-Macs/blob/master/CMacs/View.c#L5... I would definitely ask permission and encourage the author to change the license before using this code. Since API calls are purely functional and not covered by copyright you can mimic the behavior here but you'll need to rewrite everything from scratch, notably the…

I always see the strangest fluctuations in upvotes and downvotes whenever I state the laws and doctrines of copyright.

FWIW, I do court ordered code inspections to assess alleged copyright infringement for a living.

Re: C-Macs – a pure C macOS application

#87
post #78

This app uses objc_msgSend, which feels a lot like cheating, but if you simply want to avoid using nibs (while still using Objective-C or Swift), check out my NiblessMenu project and my "Working without a nib" blog series. https://github.com/lapcat/NiblessMenu https://lapcatsoftware.com/articles/working-without-a-nib-pa...

You can probably do a "true" pure C application (as in no Cocoa at all and no linking to objc-runtime) by dropping down to the CoreGraphics layer, but a lot of it is undocumented.

There's also Carbon (which I always assumed directly hit the same underlying quartz apis), but that's long deprecated and iirc never supported the UI portions in 64-bit

Re: C-Macs – a pure C macOS application

#90
post #30

I did a straight C++ app for MacOS but 1) I used SDL2 and 2) it was a full-screen game so no Cocoa UI needed. It was kind of fun though in a retro-computing way. (I'm a big fan of SDL now.)

The linked project doesn't use any ObjC files at all. SDL2 has a bunch of Cocoa files[1] so you did use Cocoa even if unknowingly. [1] https://github.com/libsdl-org/SDL/tree/main/src/video/cocoa

Yeah, that's okay. It wasn't like a "purity thing" for me, just nice to write portable code.
Post reply on HN