Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating window application in pure c on mac osx

Tags:

I'm creating an application in pure C on Mac OSX. What I want is to create window in witch my app will be stored.

Preferably I want it to be pure C solution, but if I have to use objective-c class to init window and then send context to my C code then it will be fine.

I'm not using xcode, only simple text editor in with I tried to import cocoa but it just generated a lot of errors.

So in summary my question is: How in simple pure C generate code that will display osx window?

like image 733
Mago Avatar asked May 15 '15 21:05

Mago


People also ask

Can you write Mac apps in C?

With this knowledge, we can build a simple GUI application that runs on MAC OSX using less than 50 lines of code in a single C program. This program can then later be turned into a bundle for Mac OSX to be able to open and close directly. Installing GTK on your setup to compile your GUI.

Can you make Mac apps with C++?

To run a program in C++ in Mac we have to install Xcode or command-line tools for Xcode. Ways: Hence, there are two options to run a C++ program on Mac. Download and install by using Command Line Tools and using any preferred IDE or Code Editor for writing C or C++ code.


1 Answers

I did a translation of the accepted answer to Pure C:

// based on https://stackoverflow.com/a/30269562 // 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)  // poor man's bindings! typedef enum NSApplicationActivationPolicy {     NSApplicationActivationPolicyRegular   = 0,     NSApplicationActivationPolicyAccessory = 1,     NSApplicationActivationPolicyERROR     = 2, } NSApplicationActivationPolicy;  typedef enum NSWindowStyleMask {     NSWindowStyleMaskBorderless     = 0,     NSWindowStyleMaskTitled         = 1 << 0,     NSWindowStyleMaskClosable       = 1 << 1,     NSWindowStyleMaskMiniaturizable = 1 << 2,     NSWindowStyleMaskResizable      = 1 << 3, } NSWindowStyleMask;  typedef enum NSBackingStoreType {     NSBackingStoreBuffered = 2, } NSBackingStoreType;  int main(int argc, char *argv[]) {     // id app = [NSApplication sharedApplication];     id app = cls_msg(cls("NSApplication"), sel("sharedApplication"));      // [app setActivationPolicy:NSApplicationActivationPolicyRegular];     msg(app, sel("setActivationPolicy:"), NSApplicationActivationPolicyRegular);      struct CGRect frameRect = {0, 0, 600, 500};      // id window = [[NSWindow alloc] initWithContentRect:frameRect styleMask:NSWindowStyleMaskTitled|NSWindowStyleMaskClosable|NSWindowStyleMaskResizable backing:NSBackingStoreBuffered defer:NO];     id window = msg(cls_msg(cls("NSWindow"), sel("alloc")),                     sel("initWithContentRect:styleMask:backing:defer:"),                     frameRect,                     NSWindowStyleMaskTitled|NSWindowStyleMaskClosable|NSWindowStyleMaskResizable,                     NSBackingStoreBuffered,                     false);     msg(window, sel("setTitle:"), cls_msg(cls("NSString"), sel("stringWithUTF8String:"), "Pure C App"));      // [window makeKeyAndOrderFront:nil];     msg(window, sel("makeKeyAndOrderFront:"), nil);      // [app activateIgnoringOtherApps:YES];     msg(app, sel("activateIgnoringOtherApps:"), true);      msg(app, sel("run")); } 
like image 162
hasen Avatar answered Sep 25 '22 08:09

hasen