Google protobuf的安装及使用

简介: [cpp] view plaincopy   最近应为工作的需要,合作的部门提供了protobuf的接口,总结了一下使用的过程和方法如下:                    下载protobuf-2.
[cpp]  view plain copy
 
  1. 最近应为工作的需要,合作的部门提供了protobuf的接口,总结了一下使用的过程和方法如下:  
  2.   
  3.    
  4.   
  5.    
  6.   
  7. 下载protobuf-2.3.0:  
  8.     http://protobuf.googlecode.com/files/protobuf-2.3.0.zip  
  9. 安装:   
  10. unzip protobuf-2.3.0.zip  
  11. cd protobuf-2.3.0  
  12. ./configure  
  13. make   
  14. make check   
  15. make install  
  16.   
  17. 结果:  
  18. Libraries have been installed in:  
  19.    /usr/local/lib  
  20. Head files hava been installed in:  
  21. /usr/local/include/google/  
  22. protobuf/  
  23.   
  24.   
  25. 开始写.proto文件:  
  26. BaseMessage.proto:  
  27. message MessageBase  
  28. {  
  29.     required int32 opcode = 1;  
  30.     // other: sendMgrId, sendId, recvMgrId, recvId, ...  
  31. }  
  32.   
  33. message BaseMessage  
  34. {  
  35.     required MessageBase msgbase = 1;  
  36. }  
  37.   
  38. BaseMessage.proto是其它消息proto文件的基础,以容器模块的C2S_GetContainerInfo为例:  
  39. ContainerMessage.proto:  
  40. import "BaseMessage.proto";  
  41.   
  42. message C2SGetContainerInfoMsg  
  43. {  
  44.     required MessageBase msgbase = 1;  
  45.     optional int32 containerType = 2;  
  46. }  
  47.   
  48. .proto文件编写规则:  
  49. 1)所有消息都需要包含msgbase这项,并编号都为1,即:  
  50.   required MessageBase msgbase = 1;  
  51. 2)除了msgbase这项写成required外,其它所有项都写成optional。  
  52.   
  53. 编译 .proto 文件  
  54. protoc -I=. --cpp_out=. BaseMessage.proto  
  55. protoc -I=. --cpp_out=. ContainerMessage.proto  
  56. 生成BaseMessage.pb.h、BaseMessage.pb.cc  
  57.     ContainerMessage.pb.h、ContainerMessage.pb.cc  
  58. 将它们添加到工程文件中。  
  59.   
  60. 编写C++代码:  
  61. 1)发送消息:  
  62. C2SGetContainerInfoMsg msg;  
  63. msg.mutable_msgbase()->set_opcode(C2S_GetContainerInfo);  
  64. msg.set_containertype(1);  
  65. std::string out = msg.SerializeAsString();  
  66. send(sockfd, out.c_str(), out.size(), 0);  
  67. 2)接收消息  
  68. char buf[MAXBUF + 1];  
  69. int len;  
  70. bzero(buf, MAXBUF + 1);  
  71. len = recv(new_fd, buf, MAXBUF, 0);  
  72. if (len > 0)  
  73. {  
  74.     printf("%d接收消息成功:'%s',共%d个字节的数据/n",  
  75.             new_fd, buf, len);  
  76.   
  77.     BaseMessage baseMsg;  
  78.     std::string data = buf;  
  79.     baseMsg.ParseFromString(data);  
  80.   
  81.     int opcode = baseMsg.mutable_msgbase()->opcode();  
  82.   
  83.     printf("opcode=%d/n", opcode);  
  84.   
  85.     switch (opcode)  
  86.     {  
  87.     case C2S_GetContainerInfo:  
  88.     {  
  89.         C2SGetContainerInfoMsg msg;  
  90.         msg.ParseFromString(data);  
  91.   
  92.         printf("containerType=%d/n", msg.containertype());  
  93.   
  94.         break;  
  95.     }  
  96.     default:  
  97.     {  
  98.         break;  
  99.     }  
  100.     }  
  101. }  
  102. else  
  103. {  
  104.     if (len < 0)  
  105.         printf("消息接收失败!错误代码是%d,错误信息是'%s'/n",  
  106.              errno, strerror(errno));  
  107.     close(new_fd);  
  108.     return -1;  
  109. }  
  110.   
  111.   
  112. 编译C++代码:  
  113. Need to link lib:  
  114. protobuf  
  115. pthread  
  116.   
  117.   
  118. 参考:   
  119. 1,http://www.360doc.com/content/10/0822/16/11586_47942017.shtml  
  120. 2,http://code.google.com/p/protobuf/  
  121.   
  122.    

 

原文地址:http://blog.csdn.net/ganghust/article/details/6115283

 

 

 

项目主页:http://code.google.com/p/protobuf/

下载:http://code.google.com/p/protobuf/downloads/list protobuf-2.4.1.tar.gz

 

1、./configure(注:默认可能会安装在/usr/local目录下,可以加--prefix=/usr来指定安装到/usr/lib下,可以免去路径的设置,路径设置见Linux命令pkg-config

2、make

3、make check

4、make install(需要超级用户root权限)

目录
相关文章
|
Web App开发
kali2022.1安装google chrome develop 专业版
kali2022.1安装google chrome develop 专业版
157 1
|
Web App开发 Ubuntu 安全
【已解决】ubuntu 16.04安装最新版本google chrome出错, 旧版本chrome浏览器安装流程
【已解决】ubuntu 16.04安装最新版本google chrome出错, 旧版本chrome浏览器安装流程
1813 2
|
定位技术 数据库
Win之Software Installation:谷歌地球(Google Earth) 的简介、安装、使用方法之详细攻略
Win之Software Installation:谷歌地球(Google Earth) 的简介、安装、使用方法之详细攻略
Win之Software Installation:谷歌地球(Google Earth) 的简介、安装、使用方法之详细攻略
解决kali安装不了Google输入法的原因
解决kali安装不了Google输入法的原因
152 0
|
编解码 Java C#
Google Protobuf 使用介绍
Google Protobuf 使用介绍
148 0
|
Shell
(Mac)remotedebug-ios-webkit-adapter 无法运行,出现以下错误:ios_webkit_debug_proxy找不到。请安装 ios_webkit_debug_proxy (https://github.com/google/ios-webkit-debug-proxy) #74
(Mac)remotedebug-ios-webkit-adapter 无法运行,出现以下错误:ios_webkit_debug_proxy找不到。请安装 ios_webkit_debug_proxy (https://github.com/google/ios-webkit-debug-proxy) #74
865 0
|
存储 编解码 JSON
第 7 章 Google Protobuf
第 7 章 Google Protobuf
170 0
|
测试技术 编译器 开发工具
C++服务性能优化的道与术-道篇:google benchmark的安装与使用
如果你实现一个公共的工具函数,有多种实现方式,你怎么测试性能呢?是循环多少次,然后打印一下起止时间,计算耗时吗?这样当然没问题。但是每次都类似的需求,都会写很多冗余的代码来进行耗时统计,另外也缺乏灵活性。有没有方便的方式来测试呢?有,Google家的benchmark性能测试框架。
1275 2
C++服务性能优化的道与术-道篇:google benchmark的安装与使用
|
Java Maven Python
Google protobuf在python中的应用研究
Google protobuf在python中的应用研究
417 0
Google protobuf在python中的应用研究
【Google Play】APK 扩展包 ( 2021年09月02日最新处理方案 | 内部测试链接 | 安装 Google Play 中带 扩展文件 的 APK 安装包 | 验证下载的扩展文件 )(二)
【Google Play】APK 扩展包 ( 2021年09月02日最新处理方案 | 内部测试链接 | 安装 Google Play 中带 扩展文件 的 APK 安装包 | 验证下载的扩展文件 )(二)
222 0
【Google Play】APK 扩展包 ( 2021年09月02日最新处理方案 | 内部测试链接 | 安装 Google Play 中带 扩展文件 的 APK 安装包 | 验证下载的扩展文件 )(二)

热门文章

最新文章

推荐镜像

更多