2011-09-19 74 views
16

編譯iOS的Objective-C的命令行應用程序這是一個非常簡單的Objective-C的控制檯應用程序:通過GCC在Mac

#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; 
} 

gcc main.m -o main -ObjC -framework Foundation -framework CoreLocation我的Mac上進行編譯。

我還在我的Mac上安裝了iOS SDK。如何修改此命令至在我的電腦上編譯相同的代碼,用於(越獄)iOS設備

我可以通過ssh傳輸可執行文件並用ldid對其進行簽名。

回答

4

假設你不拘泥於古板的老舊GCC,故意引發了我的項目錯誤,從而進入一個位置的Xcode將揭示使用的命令行透露編譯如下:

/開發人員/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 -fascal-strings -O0 -Wmissing-prototypes -Wreturn-type -Wparentheses -Wwitwit -Wno-unused-parameter -Wunu SED-可變-Wunused值 -DDEBUG = 1 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk -gdwarf-2 -mthumb「-DIBOutlet = 屬性((IBOutlet中))」 「-DIBOutletCollection(類名)= 屬性((iboutletcollection(類名)))」 「-DIBAction =空隙)屬性((IBAction爲)」 -miphoneos版本分鐘= 3.2 -iquote [記錄文件] -I [標題列表] -iquote [更多 標題] -I [包含路徑] -fpch-preprocess -F [調試文件的目錄 的指針] -include [my prefix header] -c AppDelegate.m -o AppDelegate。 0

如果你拒絕使用Xcode,那麼我想你至少可以減少有關Interface Builder網點的內容。而爲armv6而不是v7構建可能是我的項目中的錯誤。

,然後鏈接:

/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/clang -arch 的ARMv6 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/ SDKs/iPhoneOS4.3.sdk -L [合適的東西] -F [合適的東西] -filelist [a .LinkFileList] -dead_strip -miphoneos-version-min = 3.2 -framework SystemConfiguration -framework UIKit -framework Foundation-framework CoreGraphics -o [合適的東西]

.LinkedFileList文件似乎只是一個對象文件列表,每行一個。 Xcode已經把完整的路徑放在那裏,儘管我猜想相對路徑是可以接受的。

我感謝這不是一個完整的答案,但希望它有幫助嗎?

2

爲什麼不使用xcodebuild而不是直接撥打gcc

+0

編譯不是' xcodebuild「僅用於編譯Xcode項目?我想知道如何避免使用Xcode。 – Klaus

+4

哦。當然。那麼也許你可以這樣做:創建一個iOS Xcode項目,然後從命令行調用'xcodebuild'並觀察他在命令行中執行的gcc調用。那麼你應該能夠看到它使用的是什麼命令行參數...... Nad應該可以使用'gcc'來自己使用它,而不需要'xcodebuild' – AliSoftware

+0

這是** I **尋找的東西,謝謝! – To1ne

18

對於簡單的情況下,這可能是不夠好:

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

更新:

此命令不適用於在iOS命令行應用程序的工作 - 拱必須正確設置。目前我手頭沒有iOS開發環境;可能這個-march標誌會有所幫助。

查看接受的答案。

+0

不錯!這是有效的;) – diegocaro

+0

你也可以用'clang'替換'gcc'而不改變其他的東西。 –

+1

@keith:是的,這可能是未來的正確答案。蘋果可能會在某個時候停止發運gcc包裝。 – radiospiel

4

要在您的mac上編譯iOS代碼,您需要使用交叉編譯器,即在Intel平臺上運行並能夠生成ARM二進制代碼的編譯器。

Xcode確實提供了這樣的交叉編譯器。它位於(假設你使用的Xcode 4.3從App Store分發)

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/arm-apple-darwin10-llvm-gcc-4.2 

對於一個簡單的「世界你好」之類的節目中,我使用

alias c="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/arm-apple-darwin10-llvm-gcc-4.2" 
c -o hello hello.c -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.1.sdk/ 
+0

這很好用!謝謝! – daviewales