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)
- ssh root@10.0.2.2 'mkdir -p .ssh'
如果问道你iPhone root password,输入 alpine。
3). 拷贝刚才生成的公开密匙到 iPhone
- 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/viewvcfink/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
- LDFLAGS += -F"$(IPHONESDK)/System/Library/Frameworks" LDFLAGS += -F"$(IPHONESDK)/System/Library/PrivateFra
- 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) [ DISCUZ_CODE_157 ]lt; -o $@
- %.o: %.c
- $(CC) -c $(CFLAGS) [ DISCUZ_CODE_157 ]lt; -o $@
- %.o: %.cpp
- $(CXX) -x objective-c++ $(CFLAGS) [ DISCUZ_CODE_157 ]lt; -o $@
- %.nib: %.xib
- ibtool [ DISCUZ_CODE_157 ]lt; --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