Compile an iOS Objective-C command line app through gcc on mac

Klaus picture Klaus · Sep 19, 2011 · Viewed 19.5k times · Source

Here is a very simple Objective-C console app:

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    NSLog(@"Hello world!");

    [pool drain];

    return 0;
}

I compile it with gcc main.m -o main -ObjC -framework Foundation -framework CoreLocation on my Mac.

I also have the iOS SDK installed on my mac. How can I modify this command to compile the same code, on my computer, for use on a (jailbroken) iOS device ?

I could then transfer the executable through ssh and sign it with ldid.

Answer

radiospiel picture radiospiel · Feb 22, 2012

For simple cases this might be good enough:

gcc -o output -Wall -std=c99 source.m -framework Foundation -lobjc 

Update:

This command does not work for an iOS command line application - the arch must be set correctly. I have no iOS dev environment at hand currently; probably the -march flag would help.

See the accepted answer.