安装指定的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

相关文章
相关标签/搜索