tjb300
tjb300

Reputation: 115

How to compile a simple Mac OS X app from the command line

This is for my own curiosity, rather than any real purpose. I have written what I think is pretty much the world's simplest OS X application:

#include <Carbon/Carbon.h>

int main(int argc, char* argv[])
{
    WindowRef window;
    Rect position;

    position.top = 200;
    position.left = 200;
    position.bottom = 400;
    position.right = 600;

    /* Create window */
    CreateNewWindow(kDocumentWindowClass,
               kWindowStandardDocumentAttributes |  
               kWindowStandardHandlerAttribute,
               &position, &window);
    SetWindowTitleWithCFString(window, CFSTR("Test"));
    ShowWindow(window);

    /* Run the event loop */
    RunApplicationEventLoop();

    return 0;
}

If I load this up into Xcode and build it I get a working app. Working, in as much as a window comes up, I can click on it, drag it around, resize it, use the red/yellow/green buttons etc - which is about all you'd expect it to do.

What I'd like to do is build the same thing from the command line. I tried this:

gcc -o test test.c -framework Carbon

This compiles and creates an executable, but it doesn't work correctly. A window does come up, but I can't activate it, click on it or do anything with it.

Is there something simple I've missed to get it to generate the same output as Xcode?

Oh, and I know this is a Carbon app and I really shouldn't be writing such a thing, but like I say this is mostly for my own amusement and curiosity!

Upvotes: 1

Views: 1617

Answers (1)

Ken Thomases
Ken Thomases

Reputation: 90521

You need to put the application executable into an app bundle with an Info.plist file to get it to work properly. Basically, look at the app that Xcode built.

Commands like the following should work:

mkdir -p test.app/Contents/MacOS
cp test test.app/Contents/MacOS/
echo >test.app/Contents/Info.plist '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>en</string>
    <key>CFBundleExecutable</key>
    <string>test</string>
    <key>CFBundleIdentifier</key>
    <string>com.yourcompany.test</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>test</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleVersion</key>
    <string>1</string>
</dict>
</plist>'
open test.app

Upvotes: 1

Related Questions