Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

gcc

jailbreak

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.

like image 784
Klaus Avatar asked Sep 19 '11 15:09

Klaus


People also ask

Where is the gcc compiler on Mac?

In OS X, GCC is part of Xcode's command tools, so first, open the Mac App Store and install Xcode for free. Then, open Xcode, go to Xcode menu (on the menu bar) > Preferences > Downloads, and install Command Line Tools. You will get commands like gcc, make, purge...


2 Answers

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.

like image 192
radiospiel Avatar answered Oct 02 '22 09:10

radiospiel


Assuming you're not wedded to fusty old GCC, deliberately triggering errors in my project so as to get into a position where Xcode will reveal the command line used revealed the following for compilation:

/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/clang -x objective-c -arch armv6 -fmessage-length=0 -fdiagnostics-print-source-range-info -fdiagnostics-show-category=id -fdiagnostics-parseable-fixits -std=gnu99 -Wno-trigraphs -fpascal-strings -O0 -Wmissing-prototypes -Wreturn-type -Wparentheses -Wswitch -Wno-unused-parameter -Wunused-variable -Wunused-value -DDEBUG=1 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk -gdwarf-2 -mthumb "-DIBOutlet=attribute((iboutlet))" "-DIBOutletCollection(ClassName)=attribute((iboutletcollection(ClassName)))" "-DIBAction=void)attribute((ibaction)" -miphoneos-version-min=3.2 -iquote [a bookkeeping file] -I[a list of headers] -iquote [more headers] -I[an include path] -fpch-preprocess -F[pointer to directory for debug files] -include [my prefix header] -c AppDelegate.m -o AppDelegate.o

If you're declining the use of Xcode then I guess you can cut the stuff about Interface Builder outlets at the very least. And building for armv6 rather than v7 is probably an error in my project.

And then, to link:

/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/clang -arch armv6 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk -L[something appropriate] -F[something appropriate] -filelist [a .LinkFileList] -dead_strip -miphoneos-version-min=3.2 -framework SystemConfiguration -framework UIKit -framework Foundation -framework CoreGraphics -o [something appropriate]

The .LinkedFileList file seems just to be a list of object files, one per line. Xcode has put the full path to each in there, though I'd guess relative paths to be acceptable.

I appreciate this isn't a full answer, but hopefully it helps?

like image 43
Tommy Avatar answered Oct 02 '22 11:10

Tommy