安裝指定的Protobuf版本到Mac-Protobuf2.6.1

最近新項目用到了Protobuf來儲存數據,安裝時遇到了很多坑,網上也有不少把Protobuf集成到iOS系統上可是坑不少c++

下邊總結一下安裝流程:git

查看官方文檔
源碼在 https://github.com/google/protobuf , 若是不想本身編譯得到最新版本,則能夠下載官方編譯好的各個平臺的,下載地址:https://github.com/google/protobuf/releases找到本身須要的版本,例如要裝2.6.1的話下載protobuf-2.6.1.zipgithub

第一步:cd /Users/sddd/Downloads/protobuf-2.6.1ruby

第二步:運行 ./configurecurl

第三步:運行 make測試

第四步:運行 make checkui

第五步:運行 make installgoogle

如何沒有權限的話,在前邊加上sudourl

第六步:spa

是依賴庫

 

git clone https://github.com/alexeyxo/protobuf-objc.git 完成後

 

cd ~/protobuf-objc

./autogen.sh

./configure

~/protobuf-objc其實就是剛剛clone的文件目錄

進行./configure 可能會報錯,不過彆着急,先分析錯誤信息

configure: error:

ERROR: protobuf headers are required.

You must either install protobuf from google,

or if you have it installed in a custom location

you must add '-Iincludedir' to CXXFLAGS

and '-Llibdir' to LDFLAGS.

If you did not specify a prefix when installing

protobuf, try

'./configure CXXFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/lib'

In some 64-bit environments, try LDFLAGS=-L/usr/local/lib64.

仔細看,不難發現終端給出瞭解決辦法,我想這應該是跟系統是否是64位有關吧(我的猜想)。

./configure CXXFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/lib

運行經過後,

make

make install

最終生成的插件名字爲protoc-gen-objc,會被安裝到/usr/local/bin/目錄下。

你能夠

cd /usr/local/bin/

ls -a

按照個人方法,確定能看見protoc-gen-objc。

若是沒有make命令的話:

brew -v

若是沒有安裝的話ruby -e $(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)

brew install automake
brew install libtool

 

到這裏就能夠安裝成功了,具體集成到項目上能夠查看轉發的博客上

在終端protoc --version

 接下來是如何編譯一個proto的文件

1: 打開終端: cd 到放proto的文件夾下
2:輸入命令:protoc --plugin=/usr/local/bin/protoc addressbook.proto --cpp_out="./" 這個是生成c++ .h .cc文件
3:輸入命令:protoc --plugin=/usr/local/bin/protoc addressbook.proto --objc_out="./" 這個是生成oc .h .m 文件
 
若是要安裝最新版本的話,下面還有一個方案
 

首先:打開終端

brew -v

若是沒有安裝的話ruby -e $(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)

brew install automake
brew install libtool
brew install protobuf
就是利用brew下載安裝了。protobuf就是咱們想要的,另外兩個是依賴庫
接着
git clone https://github.com/alexeyxo/protobuf-objc.git 完成後

cd ~/protobuf-objc

./autogen.sh

./configure

~/protobuf-objc其實就是剛剛clone的文件目錄

進行./configure 可能會報錯,不過彆着急,先分析錯誤信息

configure: error:

ERROR: protobuf headers are required.

You must either install protobuf from google,

or if you have it installed in a custom location

you must add '-Iincludedir' to CXXFLAGS

and '-Llibdir' to LDFLAGS.

If you did not specify a prefix when installing

protobuf, try

'./configure CXXFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/lib'

In some 64-bit environments, try LDFLAGS=-L/usr/local/lib64.

仔細看,不難發現終端給出瞭解決辦法,我想這應該是跟系統是否是64位有關吧(我的猜想)。

./configure CXXFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/lib

運行經過後,

make

make install

最終生成的插件名字爲protoc-gen-objc,會被安裝到/usr/local/bin/目錄下。

你能夠

cd /usr/local/bin/

ls -a

按照個人方法,確定能看見protoc-gen-objc。

一切準備就緒,咱們來測試下。

在桌面建立一個 ProtoBuf的文件夾。而後

cd ~/Desktop/ProtoBuf

protoc --plugin=/usr/local/bin/protoc-gen-objc person.proto --objc_out=./

protoc會自動在/usr/local/bin/目錄下尋找名爲」protoc-gen-objc」的插件,並使用該插件編譯.proto文件,最終生成兩個文件:

Person.pb.h

Person.pb.m

 

推薦比較好的博客:

 

http://www.jianshu.com/p/d5642a7d1e10?nomobile=yes

 

http://www.jianshu.com/p/cbbb0bfd0bb6

相關文章
相關標籤/搜索