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

Joh*_*ien 6 macos bundle launchd app-bundle

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

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

$ ls -R HelloWorld.app
Contents

HelloWorld.app/Contents:
Info.plist MacOS      PkgInfo

HelloWorld.app/Contents/MacOS:
helloworld
Run Code Online (Sandbox Code Playgroud)

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

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

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

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

现在为了奇怪的行为.我跑的时候

open ./HelloWorld.app
Run Code Online (Sandbox Code Playgroud)

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

这是在OS X 10.9.2上.

我究竟做错了什么?

Alb*_*ert 3

您需要向 Cocoa 注册,将您的应用程序标记为响应式且“就绪”。如果您启用停靠图标,则意味着它停止弹跳。就您而言,如果您从扩展坞中隐藏图标,您仍然需要向 Cocoa 注册。

您可以通过创建一个类来做到这一点NSApplication。请参阅此处了解一些低级优惠。