Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

petermopar

macrumors newbie
Original poster
Jul 27, 2011
4
0
baltimore
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];
}
 
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
 

Attachments

  • urtitle_demo.zip
    2.3 KB · Views: 128
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.