protobuf

protobuf是古狗在08年出的将数据序列化和反序列化库,跨平台跨语言。现在proto3和proto2还是有一些不一样的,proto3支持更多语言和功能同时舍弃了一些复杂的语法,更强调约定而弱化了语法,对快速编写proto有很大帮助。
这也是我在原单位一直推崇的一种设计,将所有产品共有,相似的地方进行统一和实现跨语言跨平台,特别是协议和算法,目前这种设计方式有个新名词叫“中台”—-相对于前台和后台来说的。

【安装protobuf】
1,https://github.com/protocolbuffers/protobuf 下载protobuf源码
2,unzip protobuf-master.zip
3,./autogen.sh 可能需要安装一些库
4,./configure 按默认配置
5,make
6,make install
在make期间出现个错误:

virtual memory exhausted: 无法分配内存
Makefile:4019: recipe for target ‘google/protobuf/descriptor.lo’ failed
make[2]: *** [google/protobuf/descriptor.lo] Error 1
make[2]: Leaving directory ‘/home/share/protobuf/protobuf-master/src’
Makefile:1716: recipe for target ‘all-recursive’ failed
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory ‘/home/share/protobuf/protobuf-master’
Makefile:1623: recipe for target ‘all’ failed
make: *** [all] Error 2
目测是swap分区空间不足
运行swapon -s发现swap分区已全部占用没有释放
运行sudo swapoff /dev/sda5
运行sudo swapon /dev/sda5 重新挂载swap分区
解决swap分区不释放的办法还有一个就是重启。

【protobuf-c】
因为我用c语言,自然要找c的protobuf库了。至于其它语言的安装可网上搜索下,很多,语言越高级安装越简单基本都是傻瓜式安装。
1,https://github.com/protobuf-c/protobuf-c 下载protobuf-c的c库
2,unzip protobuf-c-master.zip
3,/autogen.sh
4,./configure –prefix=/home/share/protobuf/install_c/
5,make
6,make install
在这个期间遇到比较棘手的几个问题,耗时比较长的是make时候一直报错configure: error: required protobuf header file not found
网上查询了一些结果,基本都是说环境变量的问题,其实也意识到了,但是配置好后还是有问题,我重新编译了protobuf,因为之前config的时候自己加了一些配置,后来按默认就不报错了,怀疑应该是有些环境变量没有配置完全。

【测试】
1,编写test.proto
2,编译../install_c/bin/protoc-c test.proto –c_out=.
3,生成test.pb-c.c test.pb-c.h
4,编写main调用test.pb-c.h的结构和函数进行数据的序列化和反序列化。

上一篇:如何在Visual studio中修改所使用C#语言的版本


下一篇:Oracle的学习一:安装与卸载、sql *plus常用命令、Oracle用户管理