Google protobuf的安装及使用

[cpp] view plaincopy
 
  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权限)

原文地址:https://www.cnblogs.com/lidabo/p/4994264.html