centos6.5 64位 protobuf和protobuf-c源文件编译&安装

以前在windows上玩过protobuf,感觉挺简单的,这次linux项目上要使用protobuf,找了半天资料都很老,加上google网站不能访问,太TMD坑爹了。当中各种错误,加上英文资料好久没看,有点陌生了,折腾了一天终于搞定,以方便大家少走弯路。如果有补充欢迎留言。

一,安装protobuf
下载源文件:https://github.com/google/protobuf
我下载的版本是v3.0.0


解压protobuf
unzip protobuf-master.zip


下载并解压gmock
https://github.com/paulsapps/gmock-1.7.0
unzip gmock-1.7.0.zip


把gmock解压出来的目录拷贝到protobuf目录下,改名为gmock
mv gmock-1.7.0 gmock


修改protobuf下的autogen.sh文件,取消从google获取gmock操作
cd protobuf-master
vim autogen.sh
把下面这段脚本注释掉
#if test ! -e gmock; then
#  echo "Google Mock not present.  Fetching gmock-1.7.0 from the web..."
#  curl $curlopts -O https://googlemock.googlecode.com/files/gmock-1.7.0.zip
#  unzip -q gmock-1.7.0.zip
#  rm gmock-1.7.0.zip
#  mv gmock-1.7.0 gmock
#fi


开始编译,执行下面的命令:
./autogen.sh
./configure
make
make check
make install


编译时间比较长,需要耐心等待。任何环节出错,都可能会安装不成功,按照英文网站说明make check环节出错可以执行make install,但是不保证能正常运行。



二,安装protobuf-c
protobuf-c是protobuf的C语言版本
下载源文件:https://github.com/protobuf-c/protobuf-c
我下载的版本是v1.1.1


解压protobuf-c
unzip protobuf-c-master.zip
cd protobuf-c-master


增加环境变量PKG_CONFIG_PATH,否则编译的时候会提示找不到protobuf
vim /etc/profile

增加
PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

让环境变量生效
source /etc/profile


编译:
./autogen.sh
./configure
make
make install



三,使用:
安装了protobuf-c之后,会把头文件、库文件(libprotobuf-c.a)、可执行文件(protoc-c)拷贝到系统目录下。


把proto文件转换成.h和.c文件
protoc-c --c_out=. test.proto


在当前目录下会生成文件:test.pb-c.h,test.pb-c.c。


在自己测试程序中关联头文件。链接的时候记得加入库libprotobuf-c.a,-lprotobuf-c


全文完。

你可能感兴趣的:(c/c++)