使用Makefile自动编译iPhone程序

来源:互联网 发布:ubuntu卸载wine 编辑:程序博客网 时间:2024/06/11 12:45
  • 博主:易飞扬
  • 原文链接 : http://www.yifeiyang.net/iphone-development-advanced-4-use-the-makefile-to-compile-iphone-program-automatically/
  • 转载请保留上面文字。

    iPhone开发进阶(4) --- 使用Makefile自动编译iPhone程序

    Xcode 也支持以命令行形式来编译 iPhone 程序。另外还可以手动的编写 Makefile 文件,实现编译→安装的自动化批处理过程。如果你习惯了命令行的操作方式(linux,unix),那么这样的操作还是很方便的。

    首先看看 Xcode 的命令行格式:

    12
    xcodebuild -target Project_Namexcodebuild install -target Project_Name

    下面我们来实现程序的编译,并通过 ldid 转换编码格式,最后用 ssh 将编译好的程序安装到 iPhone 上的 /Applications/目录下。

    首先安装 ssh 的公开密匙到 iPhone 上

    1). 在Mac的终端上产生密匙

    12345678910
    ssh-keygen -t rsaGenerating 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): xxxEnter same passphrase again: xxxYour 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 文件

    1234567891011
    ##StrictModes yes#PubkeyAuthentication yes#AuthorizedKeysFile .ssh/authorized_keys#替换为StrictModes noPubkeyAuthentication yesAuthorizedKeysFile .ssh/authorized_keys

    5). 重新启动iPhone

    接下来,编译生成ldid工具
    123456789101112
    wget http://svn.telesphoreo.org/trunk/data/ldid/ldid-1.0.610.tgztar -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 -p0cd ldid-1.0.610g++ -I . -o util/ldid{,.cpp} -x c util/{lookup2,sha1}.csudo cp -a util/ldid /usr/bin
    最后,让我们看看Makefile中都有什么

    项目中的文件如下所示:

    Classes : source code (.m .c .cpp etc)Resources : png file and other support filesProject folder : *.xib Info.plist


    123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
    Makefile: Select allPREFIX  = arm-apple-darwin9-###/////////////////////////////////////////////////////////////###                      Executable files###/////////////////////////////////////////////////////////////CC      = $(PREFIX)gccCXX     = $(PREFIX)g++LD      = $(CC)AR      = $(PREFIX)arSTRIP   = $(PREFIX)stripOBJCOPY = $(PREFIX)objcopy###################################################################################### debug/releaseDEBUG   ?= nDEVEL   ?= 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 \            -wLDFLAGS += -framework CoreFoundationLDFLAGS += -framework FoundationLDFLAGS += -framework UIKitLDFLAGS += -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 OpenALLDFLAGS += -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-compareifeq ($(DEBUG), y)CFLAGS  += -O0 -g -DDEBUG_MUTEXelseCFLAGS  += -O3 -DNDEBUGifeq ($(DEVEL), y)CFLAGS  += -gendifendifCFLAGS += -F"$(IPHONESDK)/System/Library/Frameworks"CFLAGS += -F"$(IPHONESDK)/System/Library/PrivateFrameworks"####################################################################################BUILDDIR =./build/3.0SRCDIR   =./ClassesRESDIR   =./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: allall: $(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: cleanclean:    @rm -f $(OBJS) $(TARGET)    @rm -rf $(BUILDDIR)

    然后执行下面的make命令,我们就可以直接在 iPhone 上测试我们的程序了。

    123
    make install_respringmakemake install