如何从命令行编译简单的Mac OS X应用程序

时间:2015-07-26 14:57:26

标签: xcode macos

这是出于我自己的好奇心,而不是任何真正的目的。我写过我认为几乎是世界上最简单的OS X应用程序:

#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;
}

如果我将其加载到Xcode中并构建它,我会得到一个有效的应用程序。工作,就像一个窗口出现一样,我可以点击它,拖动它,调整它,使用红色/黄色/绿色按钮等 - 这是你期望它做的所有。

我想要做的是从命令行构建相同的东西。我试过这个:

gcc -o test test.c -framework Carbon

这会编译并创建一个可执行文件,但它无法正常工作。出现了一个窗口,但我无法激活它,点击它或对它做任何事情。

是否有一些简单的事情我错过了让它生成与Xcode相同的输出?

哦,我知道这是一个Carbon应用程序,我真的不应该写这样的东西,但就像我说这主要是为了我自己的娱乐和好奇心!

1 个答案:

答案 0 :(得分:1)

您需要将应用程序可执行文件放入带有Info.plist文件的应用程序包中,以使其正常工作。基本上,看看Xcode构建的应用程序。

以下命令应该有效:

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