Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Created August 13, 2020 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhellberg/2b2d559e07ec0a715ad2f73b47274790 to your computer and use it in GitHub Desktop.
Save peterhellberg/2b2d559e07ec0a715ad2f73b47274790 to your computer and use it in GitHub Desktop.
Trimmed down https://stackoverflow.com/a/59596600 to make it even more minimal
// based on https://stackoverflow.com/a/59596600
// Minimal Pure C code to create a window in Cocoa
// $ clang minimal.c -framework Cocoa -o minimal.app
#include <objc/runtime.h>
#include <objc/message.h>
#include <Carbon/Carbon.h>
#define cls objc_getClass
#define sel sel_getUid
#define msg ((id (*)(id, SEL, ...))objc_msgSend)
#define cls_msg ((id (*)(Class, SEL, ...))objc_msgSend)
int main(int argc, char *argv[])
{
id app = cls_msg(cls("NSApplication"), sel("sharedApplication"));
msg(app, sel("setActivationPolicy:"), 0);
struct CGRect frameRect = {0, 0, 512, 256};
id window = msg(
cls_msg(cls("NSWindow"), sel("alloc")),
sel("initWithContentRect:styleMask:backing:defer:"),
frameRect, 0, 2, false);
msg(window, sel("makeKeyAndOrderFront:"), nil);
msg(app, sel("activateIgnoringOtherApps:"), true);
msg(app, sel("run"));
}
@peterhellberg
Copy link
Author

Screenshot of the window in all its glory:

Screenshot 2020-08-14 at 01 23 14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment