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

  • 博主:易飞扬
  • 原文链接 : 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 的命令行格式:

     

    xcodebuild -target Project_Name
    xcodebuild install -target Project_Name

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

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

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

    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 [email protected]

    过程中会提问你通行证(passphrase),输入你常用的秘密。

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

    ssh [email protected] 'mkdir -p .ssh'

    如果问道你iPhone root password,输入 alpine。

    3). 拷贝刚才生成的公开密匙到 iPhone

    cat ~/.ssh/id_rsa.pub | ssh [email protected] '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 filesProject 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

    你可能感兴趣的:(iPhone开发)