iPhone開發進階(4) --- 使用Makefile自動編譯iPhone程式

來源:互聯網
上載者:User

Xcode 也支援以命令列形式來編譯 iPhone 程式。另外還可以手動的編寫 Makefile 檔案,實現編譯→安裝的自動化批處理過程。如果你習慣了命令列的操作方式(linux,unix),那麼這樣的操作還是很方便的。

首先看看 Xcode 的命令列格式:
 xcodebuild -target Project_Name
xcodebuild install -target Project_Name


下面我們來實現程式的編譯,並通過 ldid 轉換編碼格式,最後用 ssh 將編譯好的程式安裝到 iPhone 上的 /Applications/目錄下。

首先安裝 ssh 的公開密匙到 iPhone 上
1). 在Mac的終端上產生密匙

 ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/xxxx/.ssh/id_rsa):
Created directory '/home/xxxx/.ssh'.
Enter passphrase (empty for no passphrase): xxx
Enter same passphrase again: xxx
Your identification has been saved in /home/xxxx/.ssh/id_rsa.
Your public key has been saved in /home/xxxx/.ssh/id_rsa.pub.
The key fingerprint is:
e4:e8:b7:05:06:b3:f0:ff:af:13:fc:50:6a:5b:d1:b5 xxxx@localhost.localdomain


過程中會提問你通行證(passphrase),輸入你常用的秘密。

2). 在 iPhone 上建立.ssh目錄(iPhone的IP地址是10.0.2.2)

1 ssh root@10.0.2.2 'mkdir -p .ssh'


如果問道你iPhone root password,輸入 alpine。

3). 拷貝剛才產生的公開密匙到 iPhone

1 cat ~/.ssh/id_rsa.pub | ssh root@10.0.2.2 'cat >> .ssh/authorized_keys'


如果問道你iPhone root password,輸入 alpine。

4). 在 iPhone 上編輯 /etc/ssh/sshd_config 檔案

 #將

#StrictModes yes
#PubkeyAuthentication yes
#AuthorizedKeysFile .ssh/authorized_keys

#替換為

StrictModes no
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys


5). 重新啟動iPhone

接下來,編譯產生ldid工具
 wget http://svn.telesphoreo.org/trunk/data/ldid/ldid-1.0.610.tgz

tar -zxf ldid-1.0.610.tgz

# 如果是 PowerPC 下載下面的補丁
# wget -qO- http://fink.cvs.sourceforge.net/viewvc/*checkout*/fink/dists/10.4/unstable/crypto/finkinfo/ldid.patch?revision=1.1 | patch -p0

cd ldid-1.0.610

g++ -I . -o util/ldid{,.cpp} -x c util/{lookup2,sha1}.c

sudo cp -a util/ldid /usr/bin


最後,讓我們看看Makefile中都有什麼
項目中的檔案如下所示:

Classes : source code (.m .c .cpp etc)

Resources : png file and other support files

Project folder : *.xib Info.plist
Makefile: Select all

PREFIX  = arm-apple-darwin9-

###/////////////////////////////////////////////////////////////
###                      Executable files
###/////////////////////////////////////////////////////////////

CC      = $(PREFIX)gcc
CXX     = $(PREFIX)g++
LD      = $(CC)
AR      = $(PREFIX)ar
STRIP   = $(PREFIX)strip
OBJCOPY = $(PREFIX)objcopy

####################################################################################

## debug/release
DEBUG   ?= n

DEVEL   ?= n

## SDK版本
SDKVER    = 3.1.2

## iPhone的IP地址
IPHONE_IP = 10.0.2.2

## iPhone SDK路徑
IPHONESDK = /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS$(SDKVER).sdk

## include 路徑
INCPATH += -I"$(IPHONESDK)/usr/include"
INCPATH += -I"/Developer/Platforms/iPhoneOS.platform/Developer/usr/lib/gcc/arm-apple-darwin9/4.2/include/"
INCPATH += -I"/Developer/Platforms/iPhoneOS.platform/Developer/usr/include/"
INCPATH += -I"/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator$(SDKVER).sdk/usr/include"

## 標準庫或者架構的設定
LDFLAGS=    -lobjc \
            -bind_at_load \
            -multiply_defined suppress \
            -w

LDFLAGS += -framework CoreFoundation
LDFLAGS += -framework Foundation
LDFLAGS += -framework UIKit
LDFLAGS += -framework CoreGraphics
#LDFLAGS += -framework AddressBookUI
#LDFLAGS += -framework AddressBook
#LDFLAGS += -framework QuartzCore
#LDFLAGS += -framework GraphicsServices
#LDFLAGS += -framework CoreSurface
#LDFLAGS += -framework CoreAudio
#LDFLAGS += -framework Celestial
#LDFLAGS += -framework AudioToolbox
#LDFLAGS += -framework WebCore
#LDFLAGS += -framework WebKit
#LDFLAGS += -framework SystemConfiguration
#LDFLAGS += -framework CFNetwork
#LDFLAGS += -framework MediaPlayer
#LDFLAGS += -framework OpenGLES
#LDFLAGS += -framework OpenAL

LDFLAGS += -F"$(IPHONESDK)/System/Library/Frameworks"
LDFLAGS += -F"$(IPHONESDK)/System/Library/PrivateFrameworks"

## 編譯開關
CFLAGS  += $(INCPATH) \
        -std=c99 \
        -W -Wall \
        -funroll-loops \
        -Diphoneos_version_min=2.0 \
        -Wno-unused-parameter \
        -Wno-sign-compare

ifeq ($(DEBUG), y)
CFLAGS  += -O0 -g -DDEBUG_MUTEX
else
CFLAGS  += -O3 -DNDEBUG
ifeq ($(DEVEL), y)
CFLAGS  += -g
endif
endif

CFLAGS += -F"$(IPHONESDK)/System/Library/Frameworks"
CFLAGS += -F"$(IPHONESDK)/System/Library/PrivateFrameworks"

####################################################################################

BUILDDIR =./build/3.0
SRCDIR   =./Classes
RESDIR   =./Resources

###/////////////////////////////////////////////////////////////
###                        Source files
###/////////////////////////////////////////////////////////////

OBJS     = $(patsubst %.m,%.o,$(wildcard $(SRCDIR)/*.m))
OBJS    += $(patsubst %.m,%.o,$(wildcard ./*.m))
OBJS    += $(patsubst %.c,%.o,$(wildcard $(SRCDIR)/*.c))
OBJS    += $(patsubst %.cpp,%.o,$(wildcard $(SRCDIR)/*.cpp))

NIBS     = $(patsubst %.xib,%.nib,$(wildcard *.xib))

RESOURCES= $(wildcard $(RESDIR)/*)

APPFOLDER= $(TARGET).app

.PHONY: all
all: $(TARGET) bundle

$(TARGET): $(OBJS)
    $(LD) $(LDFLAGS) -o $@ $^

%.o:    %.m
    $(CC) -c $(CFLAGS) $< -o $@

%.o:    %.c
    $(CC) -c $(CFLAGS) $< -o $@

%.o:    %.cpp
    $(CXX) -x objective-c++ $(CFLAGS) $< -o $@

%.nib:  %.xib
    ibtool $< --compile $@

bundle: $(TARGET)
    @rm -rf $(BUILDDIR)
    @mkdir -p $(BUILDDIR)/$(APPFOLDER)
    @cp -r $(RESDIR)/* $(BUILDDIR)/$(APPFOLDER)
    @cp Info.plist $(BUILDDIR)/$(APPFOLDER)/Info.plist
    @echo "APPL" > $(BUILDDIR)/$(APPFOLDER)/PkgInfo
    mv $(NIBS) $(BUILDDIR)/$(APPFOLDER)
#    export CODESIGN_ALLOCATE=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_allocate
    @ldid -S $(TARGET)
    @mv $(TARGET) $(BUILDDIR)/$(APPFOLDER)/$(TARGET)_

install: bundle
    @ssh root@$(IP) "cd /Applications/$(APPFOLDER) && rm -R * || echo 'not found' "
    @scp -rp $(BUILDDIR)/$(APPFOLDER) root@$(IP):/Applications
    @ssh root@$(IP) "cd /Applications/$(APPFOLDER) ; ldid -S $(TARGET)_; killall SpringBoard"
    @echo "Application $(APPFOLDER) installed"

uninstall:
    ssh root@$(IPHONE_IP) 'rm -fr /Applications/$(APPFOLDER); respring'
    @echo "Application $(APPFOLDER) uninstalled, please respring iPhone"

install_respring:
    scp respring_arm root@$(IPHONE_IP):/usr/bin/respring

.PHONY: clean
clean:
    @rm -f $(OBJS) $(TARGET)
    @rm -rf $(BUILDDIR)


然後執行下面的make命令,我們就可以直接在 iPhone 上測試我們的程式了。

make install_respring
make
make install


 作者:易飛揚

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.