[iOS] 使用 Shell Command 編譯 helloworld

  • 1439
  • 0

摘要:[iOS] 使用 shell 編譯 helloworld

此篇主要是最近在研讀有關iOS資安方面的書籍

所以會運用shell來做些原本只要按按鈕就可以達成的簡單事情

比如說運用shell command 來編譯我們寫好的一個hello.c的檔案

首先把我們的hello.c 建立起來

可能長成下面這樣:


#include 
main()
{
    printf("Hello World! ^.<  \n");
}

使用的編譯器是 llvm-gcc 編譯器,來編譯hello.c

首先找到我們的編譯器

我是使用Xcode 4.6 

編譯器的位置在 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/

的目錄下面

$ cd  /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/

$ ls *llvm-gcc*

arm-apple-darwin10-llvm-gcc-4.2
i686-apple-darwin10-llvm-gcc-4.2
llvm-gcc
llvm-gcc-4.2

 

找到編譯器後

繼續下指令

$export PLATFORM=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform 

 

$PLATFORM/Developer/usr/bin/arm-apple-darwin10-llvm-gcc-4.2 \
 -o hello hello.c \
 -isysroot $PLATFORM/Developer/SDKs/iPhoneOS6.1.sdk

注意:下上面兩個指令之前要先cd (change directory)到你存hello.c的資料夾,否則會找不到檔案

 

成功的話會在你放hello.c的資料夾裡面多一個 名叫 hello的執行檔(Unix Executable File)

再使用 file 指令檢查

 

$ file hello
 
hello: Mach-O executable arm
 
這樣就算是完成了~
 
 
 
 
 
之後會再用otool把這個檔案反組譯
 
 
 
今天這個是小練習...書後面還有蠻多東西要研究的....哈哈哈....
 
之後有練習的會再分享出來
 
P.S. 書名叫做 "iOS 安全防護" O'RELLY出版,有興趣的人可以翻翻~