how to make gcc compile a "full" output app

Discussion in 'Mac Programming' started by petermopar, Jul 27, 2011.

  1. petermopar macrumors newbie

    Joined:
    Jul 27, 2011
    Location:
    baltimore
    #1
    so take some code (at bottom) and compile it in gcc, like so:
    gcc -framework Foundation -framework Cocoa untitled.m -ObjC -o test

    now the output is test. run this from the terminal, and the process is still named "terminal". also running it by double clicking it does not allow it "to be itself" but it is still "terminal".

    of course xcode creates apps that have their own name and don't depend on terminal to open. compiling the same code in xcode, it creates a product which looks like an app but is actually a wrapper around a few directories and a binary. even going down to this binary and double clicking it, it has its own name and icon in the dock.

    what flags are needed to make gcc have this crucial output attribute? it's important because my program is capturing command-s etc, and instead of getting it, terminal gets it... thanks so much!

    Code:
    #import <Cocoa/Cocoa.h>
    
    int main(int argc, char *argv[]) {
    	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    	[NSApplication sharedApplication];
    	int style = NSClosableWindowMask | NSResizableWindowMask |
    	NSTexturedBackgroundWindowMask | NSTitledWindowMask | NSMiniaturizableWindowMask;
    	NSWindow *win = [[NSWindow alloc] initWithContentRect:NSMakeRect(50, 50, 600, 400)
    												styleMask:style
    												  backing:NSBackingStoreBuffered
    													defer:NO];
    	[win makeKeyAndOrderFront:win];
    	[NSApp run];
    	
    	[pool release];
    }
    
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    A .app is just a directory will a particular structure. You just need to re-create that structure.

    You might want to read the first 3 chapters of the Bundle Programming Guide.
     
  3. petermopar thread starter macrumors newbie

    Joined:
    Jul 27, 2011
    Location:
    baltimore
    #3
    application bundle, Info.plist, gcc Makefile

    thanks again jiminaus.
    in the interest of making max bang out of this info, i have prepared three demo files to help people make a bare-bones app that truly runs right in OSX. no nibs.

    first, the "urtitle.m" objective c code (same as last time):
    Code:
    #import <Cocoa/Cocoa.h>
    
    int main(int argc, char *argv[]) {
    	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    	[NSApplication sharedApplication];
    	int style = NSClosableWindowMask | NSResizableWindowMask |
    		NSTexturedBackgroundWindowMask | NSTitledWindowMask |
    		NSMiniaturizableWindowMask;
    	NSWindow *win = [[NSWindow alloc] 
    		initWithContentRect:NSMakeRect(50, 50, 600, 400)
    		styleMask:style
    		backing:NSBackingStoreBuffered
    		defer:NO];
    	[win makeKeyAndOrderFront:win];
    	[NSApp run];
    	
    	[pool release];
    }
    now we have a makefile to compose the directory structure and output the executable binary into its depths (makefile):
    Code:
    all:
    	mkdir urtitle.app
    	mkdir urtitle.app/Contents
    	mkdir urtitle.app/Contents/MacOS
    	gcc urtitle.m -framework Cocoa -framework Foundation -o urtitle.app/Contents/MacOS/urtitle
    	cp Info.plist urtitle.app/Contents/
    clean:
    	rm -r *.app
    
    note that Info.plist is copied into urtitle.app/Contents. Info.plist has many keys, but the most important name is that the CFBundleExecutable is urtitle, and it exists down there in urtitle.app/Contents/MacOS ... OSX complains that the app is broken if the executable name is different from the top directory name. I grabbed an Info.plist from an Xcode project and was able to pare it down to three entries (Info.plist must be properly capitalized):

    Code:
    <plist>
    <dict>
    
    	<key>CFBundleExecutable</key>
    	<string>urtitle</string>
    
    	<key>CFBundlePackageType</key>
    	<string>APPL</string>
    
    
    	<key>NSPrincipalClass</key>
    	<string>NSApplication</string>
    </dict>
    </plist>
    hope this helps anyone else. urtitle_demo.zip attached with the three philes. thanks for the ref jiminaus.

    -pb
     

    Attached Files:

Share This Page