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. Openfire:安装指南

    本文的英文原文来自 http://www.igniterealtime.org/builds/openfire/docs/latest/documentation/install-guide.html ...

  2. javascript DOM 节点 第18节

    <html> <head> <title>DOM对象</title> </head><body><div >DOM对 ...

  3. Headfirst设计模式的C++实现——工厂方法(Factory Method)

    引用原书的一句话:所有的工厂模式都用来封装对象的创建,工厂方法模式通过让子类决定该创建的对象是什么来达到封装的目的. Pizza类及其派生类与上一例相同 PizzaStore.h #ifndef _P ...

  4. Android从服务端获取json解析显示在客户端上面

    Android从服务端获取json解析显示在客户端上面 百度经验:jingyan.baidu.com 首先说一下Json数据的最基本的特点,Json数据是一系列的键值对的集合,和XML数据来比,Jso ...

  5. gitlab ActionView::Template::Error (undefined method `[]' for nil:NilClass): 500错误

    Started GET "/mygroup/myproject/tree/master/MyDirectory" for 127.0.0.1 at 2014-10-22 22:42 ...

  6. Git---Git及GitHub使用笔记

    一.远程项目获取(克隆) syntax: $ git clone <版本库的网址> $ git clone <版本库的网址> <本地目录名> example: $ ...

  7. linux shell 逻辑运算符

    一.逻辑卷标 逻辑卷标 表示意思 1. 关于档案与目录的侦测逻辑卷标! -f 常用!侦测『档案』是否存在 eg: if [ -f filename ] -d 常用!侦测『目录』是否存在 -b 侦测是否 ...

  8. odoo 清除所有运行数据

    测试odoo,如果需要一个干净的db.经常需要清除掉所有业务数据.做如下操作,较为方便 1:建立一个服务器动作,动作的python代码入下. 然后新建一个菜单,菜单动作关联到 这个动作.需要清空db, ...

  9. share my tools With Xcode

    1.让Xcode的控制台支持LLDB类型的打印 在Xcode断点调试的时候, 在控制台输入 po self.view.frame 或者 po id 类型的时候就死翘翘了. 进入正题: 安装LLDB调试 ...

  10. APP 如何适应 iPhone 5s/6/6Plus 三种屏幕的尺寸

    初代iPhone 2007年,初代iPhone发布,屏幕的宽高是 320 x 480 像素.下文也是按照宽度,高度的顺序排列.这个分辨率一直到iPhone 3GS也保持不变. 那时编写iOS的App( ...