首页 文章

在Mac OS X应用程序包中打包C二进制文件

提问于
浏览
6

我正在尝试将我的二进制文件打包到一个简约的应用程序包中 . 但是我看到结果有些奇怪的行为 .

我的包有这个最小的结构:

$ ls -R HelloWorld.app
Contents

HelloWorld.app/Contents:
Info.plist MacOS      PkgInfo

HelloWorld.app/Contents/MacOS:
helloworld

helloworld是一个C二进制文件,编译自:

#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv) {
    while (1) {
        printf("Hello world!\n");
        sleep(2);
    }

    return 0;
}

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>CFBundleExecutable</key>
    <string>helloworld</string>
    <key>CFBundleIdentifier</key>
    <string>com.litl.helloworld</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>HelloWorld</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0.0</string>
    <key>CFBundleVersion</key>
    <string>20</string>
    <key>LSMinimumSystemVersion</key>
    <string>10.6</string>
    <key>LSUIElement</key>
    <true/>
    <key>LSBackgroundOnly</key>
    <true/>
</dict>
</plist>

现在为了奇怪的行为 . 当我跑

open ./HelloWorld.app

该命令挂起约30秒 . 之后我可以确认helloworld二进制文件正在运行 . 但是它的标准输出不会出现在Console.app中 . 如果我以编程方式启动此捆绑包(NSWorkspace sharedWorkspace)launchApplicationAtURL ...)调用成功,但二进制文件立即退出(我可以在控制台中看到它退出时出现错误代码2) .

这是在OS X 10.9.2上 .

我究竟做错了什么?

1 回答

  • 2

    您需要向Cocoa注册,以便将您的应用程序标记为响应并“准备好” . 如果您启用停靠栏图标,则表示停止反弹 . 在您的情况下,如果您从Dock中隐藏图标,则仍需要向Cocoa注册 .

    你可以这样做,例如通过创建 NSApplication 类 . 对于某些低级别的deails,请参见here .

相关问题