Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Packaging C binary in Mac OS X Application Bundle

I'm trying to package my binary in a minimalistic app bundle. But I'm seeing some strange behavior with the result.

My bundle has this minimal structure:

$ ls -R HelloWorld.app
Contents

HelloWorld.app/Contents:
Info.plist MacOS      PkgInfo

HelloWorld.app/Contents/MacOS:
helloworld

helloworld is a C binary compiled from:

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

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

    return 0;
}

Info.plist contains:

<?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>

Now for the strange behavior. When I run

open ./HelloWorld.app

The command hangs for about 30s. After that I can confirm that the helloworld binary is running. However its standard output does not show up in Console.app. If I launch this bundle programmatically (NSWorkspace sharedWorkspace] launchApplicationAtURL...) the call succeeds, but the binary exits immediately (I can see in the console it exited with error code 2).

This is on OS X 10.9.2.

What am I doing wrong?

like image 724
Johan Bilien Avatar asked Mar 25 '14 20:03

Johan Bilien


People also ask

Does macOS use binary?

An app that supports only the x86_64 architecture must run under Rosetta translation on Apple silicon. A universal binary runs natively on both Apple silicon and Intel-based Mac computers, because it contains executable code for both architectures.

What is a OSX bundle?

Bundles are a convenient way to deliver software in macOS and iOS. Bundles provide a simplified interface for end users and at the same time provide support for development. This chapter provides an introduction to bundles and discusses the role they play in macOS and iOS.

What is aarch64 in Mac?

It is the architecture for all Intel Macs shipped between 2005 and 2021. arm64 is the architecture used by newer Macs built on Apple Silicon, shipped in late 2020 and beyond.


1 Answers

You need to register with Cocoa to mark your application as responsive and 'ready'. If you would enable the dock icon, it means that it stops to bounce. In your case, if you hide the icon from the dock, you still need to register with Cocoa.

You can do that e.g. by creating a NSApplication class. See here for some low level deails.

like image 114
Albert Avatar answered Sep 23 '22 02:09

Albert