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

原文地址: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权限)

Google protobuf的安装及使用的更多相关文章

  1. google protobuf安装与使用

    google protobuf是一个灵活的.高效的用于序列化数据的协议.相比较XML和JSON格式,protobuf更小.更快.更便捷.google protobuf是跨语言的,并且自带了一个编译器( ...

  2. mac安装protobuf2.4.1时报错./include/gtest/internal/gtest-port.h:428:10: fatal error: 'tr1/tuple' file not found和google/protobuf/message.cc:175:16: error: implicit instantiation of undefined template

    通过网上下载的protobuf2.4.1的压缩文件,然后进行安装,./configure和make时遇到了两个问题. 正常的安装步骤如下: ./configure make  make check m ...

  3. caffe安装编译问题-ImportError: No module named google.protobuf.internal

    问题描述 ~/Downloads/caffe$ python Python (default, Dec , ::) [GCC ] on linux2 Type "help", &q ...

  4. GOOGLE PROTOBUF开发者指南

    原文地址:http://www.cppblog.com/liquidx/archive/2009/06/23/88366.html 译者: gashero 目录 1   概览 1.1   什么是pro ...

  5. google protobuf ios开发使用

    简介: protobuf 即 google protocol buffer 是一种数据封装格式协议: 比如其他经常用的xml,json等格式:protobuf的优势是效率高,同样的一份数据使用prot ...

  6. google protobuf使用

    下载的是github上的:https://github.com/google/protobuf If you get the source from github, you need to gener ...

  7. 在C语言环境下使用google protobuf

    本文写给经常使用C编程且不喜欢C++而又要经常使用google protobuf的人.        经常写通讯程序的人对数据进行序列化或者反序列化时,可能经常使用google的protobuf(PB ...

  8. (原)python中import caffe提示no module named google.protobuf.internal

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5993405.html 之前在一台台式机上在python中使用import caffe时,没有出错.但是 ...

  9. VS下使用Google Protobuf完成SOCKET通信

    如何在Windows环境下的VS中安装使用Google Protobuf完成SOCKET通信 出处:如何在Windows环境下的VS中安装使用Google Protobuf完成SOCKET通信 最近一 ...

随机推荐

  1. iOS开发——UIWebView

    (已写好代码,待空闲更新……)

  2. css层叠顺序

    优先级: 1.添加!important规则的分组享有最高优先级:会将所有不带!important的规则 例如:#id{border:6px solid black!important}高于#id{bo ...

  3. redis研究之watch

    具体的看注释,代码如下: public static void TransTest() { RedisClient client1 = new RedisClient(host, port); Red ...

  4. PHP pear安装出现 Warning: require_once(Structures/Graph.php)...错误

    今天在WINDOWS安装pear,一路无阻很顺利安装完成,接着想安装下pear email包来玩下,但接下来却报: Warning: require_once(Structures/Graph.php ...

  5. java PropertyChangeSupport委托帧听类的使用

    要委托的Bean类写法. import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import ...

  6. Java学习笔记(基本数据类型和变量命名规则)

    java基本数据类型 变量 1.变量就是可变的量. 2.常量就是不可变的量. 3.字面量:Java的变量和常量中存放的具体的数据成为字面量. 变量 命名规则: (1)首字母是英文字母.$或下划线,由字 ...

  7. 2、Charm Bracelet( poj 3624)简单0-1背包

    题意:有n件手镯,总重量不能超过M,每个手镯有一个体重W[i]和魅力V[i],问在不超过M的情况下能获得的魅力总和 思路:把M当背包总容量,用0-1背包写 代码: #include <iostr ...

  8. mysql 查找包含特定名字的表

    SELECT distinct TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME LIKE '%medias%'

  9. js 中对象的特性

    用Object.definedproperties 一次性添加或修改多个属性的特性和值. <script> var obj ={} Object.defineProperties(obj, ...

  10. 支付宝开发(一)-认识php openssl RSA 非对称加密实现

    获取支付宝公钥 本地服务器生成私钥和公钥 运用php中openssl相关函数加密解密验证身份 以下是php中openssl相关函数实现的验证,来自php官方demo //需要签名的数据 $data = ...