介紹
介紹的話,不必多說了,網(wǎng)上很多內(nèi)容
安裝
- 安裝必要工具,電腦上已經(jīng)安裝的brew,通過brew install來安裝必要的工具,有autoconf,automake,libtool,protobuf,aclocal。期間可能會(huì)遇到報(bào)錯(cuò),說brew無法連接,這時(shí)需要輸入命令
sudo chown -R $(whoami) /usr/local
- 在項(xiàng)目主頁下有文檔說明,安裝說明依次輸入命令:
$ git clone https://github.com/protocolbuffers/protobuf.git
$ cd protobuf
$ git submodule update --init --recursive
$ ./autogen.sh
$ ./configure
進(jìn)行./configure 可能會(huì)報(bào)錯(cuò),不過別著急,先分析錯(cuò)誤信息
configure: error:ERROR: protobuf headers are required.You must either install protobuf from google,or if you have it installed in a custom locationyou must add '-Iincludedir' to CXXFLAGSand '-Llibdir' to LDFLAGS.If you did not specify a prefix when installingprotobuf, try'./configure CXXFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/lib'In some 64-bit environments, try LDFLAGS=-L/usr/local/lib64.
仔細(xì)看,不難發(fā)現(xiàn)終端給出了解決辦法,我想這應(yīng)該是跟系統(tǒng)是不是64位有關(guān)吧(個(gè)人猜測)。
如果
$ ./configure CXXFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/lib
運(yùn)行還是報(bào)錯(cuò),換成下面這條語句
./configure CXXFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/lib64
成功后,輸入
$ make
$ make install
沒什么意外的話,環(huán)境應(yīng)該建立起來了。
一鍵添加pb文件到Xcode項(xiàng)目里
每次都要pb文件有更新的話,都要手動(dòng)更新的話會(huì)煩不勝煩的,幸好在網(wǎng)上搜索了一下,發(fā)現(xiàn)幾篇文章有涉及到相關(guān)內(nèi)容。
參考上面幾篇文章,基本上實(shí)現(xiàn)了將pb文件一鍵添加到Xcode項(xiàng)目里,不過還有點(diǎn)缺憾,相關(guān)的.m 文件是非arc的,所以怎么實(shí)現(xiàn)在同一個(gè)ruby源文件里,實(shí)現(xiàn)add compiler flags呢?通過Google搜索,在github上找到了相關(guān)內(nèi)容。
我摘錄下來
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'xcodeproj'
# Add compiler flags to "Build Phases > Compile Sources"
def add_compiler_flags(xcproj, flags)
xcproj.targets.each do |target|
target.source_build_phase.files_references.each do |fileref|
# You can check fileref with fileref.path or fileref.parent
# fileref.parent is PBXGroup or PBXProject
# In this sample, we check if any parent group has "WinObjC/Frameworks" path
add_flags = false
r = fileref
loop {
if r.kind_of?(Xcodeproj::Project::Object::PBXProject) || r.source_tree != '<group>'
break
# 根據(jù)自己的項(xiàng)目修改path
elsif r.path == 'WinObjC/Frameworks'
add_flags = true
break
end
r = r.parent
}
next unless add_flags
fileref.build_files.each do |bf|
# TODO: merge settings with original COMPILER_FLAGS
bf.settings = {'COMPILER_FLAGS' => flags}
end
end
end
end
def main
//根據(jù)自己的內(nèi)容修改調(diào)用的代碼
xcproj_path = ARGV.shift
flags = ARGV.shift
xcproj = Xcodeproj::Project.new(xcproj_path)
xcproj.initialize_from_file
add_compiler_flags(xcproj, flags)
xcproj.save
end
main
至此折騰的差不多了。相關(guān)的文件,上面參考的文章都有相應(yīng)的內(nèi)容,如果需要更改的話,根據(jù)自己實(shí)際項(xiàng)目去修改即可。因?yàn)橄嚓P(guān)資料零零散散的,特此整理一下。