Skip to content

Instantly share code, notes, and snippets.

@pingjiang
Created August 15, 2014 09:06
Show Gist options
  • Save pingjiang/5298cf78350f5a6e9cc1 to your computer and use it in GitHub Desktop.
Save pingjiang/5298cf78350f5a6e9cc1 to your computer and use it in GitHub Desktop.
This program shows how to access Cocoa GUI from pure C/C++ and build a truly functional GUI application (although very simple).
/*
* test1.cpp
* This program shows how to access Cocoa GUI from pure C/C++
* and build a truly functional GUI application (although very simple).
*
* Compile using:
* g++ -framework Cocoa -o test1 test1.cpp
*
* that will output 'test1' binary.
*/
#include <CoreFoundation/CoreFoundation.h>
#include <objc/objc.h>
#include <objc/objc-runtime.h>
#include <iostream>
extern "C" int NSRunAlertPanel(CFStringRef strTitle, CFStringRef strMsg,
CFStringRef strButton1, CFStringRef strButton2,
CFStringRef strButton3, ...);
int main(int argc, char** argv)
{
id app = NULL;
id pool = objc_getClass("NSAutoreleasePool");
if (!pool)
{
std::cerr << "Unable to get NSAutoreleasePool!\nAborting\n";
return -1;
}
pool = objc_msgSend(pool, sel_registerName("alloc"));
if (!pool)
{
std::cerr << "Unable to create NSAutoreleasePool...\nAborting...\n";
return -1;
}
pool = objc_msgSend(pool, sel_registerName("init"));
app = objc_msgSend(objc_getClass("NSApplication"),
sel_registerName("sharedApplication"));
NSRunAlertPanel(CFSTR("Testing"),
CFSTR("This is a simple test to display NSAlertPanel."),
CFSTR("OK"), NULL, NULL);
objc_msgSend(pool, sel_registerName("release"));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment