Google protobuf的安装及使用
- 最近应为工作的需要,合作的部门提供了protobuf的接口,总结了一下使用的过程和方法如下:
- 下载protobuf-2.3.0:
- http://protobuf.googlecode.com/files/protobuf-2.3.0.zip
- 安装:
- unzip protobuf-2.3.0.zip
- cd protobuf-2.3.0
- ./configure
- make
- make check
- make install
- 结果:
- Libraries have been installed in:
- /usr/local/lib
- Head files hava been installed in:
- /usr/local/include/google/
- protobuf/
- 开始写.proto文件:
- BaseMessage.proto:
- message MessageBase
- {
- required int32 opcode = 1;
- // other: sendMgrId, sendId, recvMgrId, recvId, ...
- }
- message BaseMessage
- {
- required MessageBase msgbase = 1;
- }
- BaseMessage.proto是其它消息proto文件的基础,以容器模块的C2S_GetContainerInfo为例:
- ContainerMessage.proto:
- import "BaseMessage.proto";
- message C2SGetContainerInfoMsg
- {
- required MessageBase msgbase = 1;
- optional int32 containerType = 2;
- }
- .proto文件编写规则:
- 1)所有消息都需要包含msgbase这项,并编号都为1,即:
- required MessageBase msgbase = 1;
- 2)除了msgbase这项写成required外,其它所有项都写成optional。
- 编译 .proto 文件
- protoc -I=. --cpp_out=. BaseMessage.proto
- protoc -I=. --cpp_out=. ContainerMessage.proto
- 生成BaseMessage.pb.h、BaseMessage.pb.cc
- ContainerMessage.pb.h、ContainerMessage.pb.cc
- 将它们添加到工程文件中。
- 编写C++代码:
- 1)发送消息:
- C2SGetContainerInfoMsg msg;
- msg.mutable_msgbase()->set_opcode(C2S_GetContainerInfo);
- msg.set_containertype(1);
- std::string out = msg.SerializeAsString();
- send(sockfd, out.c_str(), out.size(), 0);
- 2)接收消息
- char buf[MAXBUF + 1];
- int len;
- bzero(buf, MAXBUF + 1);
- len = recv(new_fd, buf, MAXBUF, 0);
- if (len > 0)
- {
- printf("%d接收消息成功:'%s',共%d个字节的数据/n",
- new_fd, buf, len);
- BaseMessage baseMsg;
- std::string data = buf;
- baseMsg.ParseFromString(data);
- int opcode = baseMsg.mutable_msgbase()->opcode();
- printf("opcode=%d/n", opcode);
- switch (opcode)
- {
- case C2S_GetContainerInfo:
- {
- C2SGetContainerInfoMsg msg;
- msg.ParseFromString(data);
- printf("containerType=%d/n", msg.containertype());
- break;
- }
- default:
- {
- break;
- }
- }
- }
- else
- {
- if (len < 0)
- printf("消息接收失败!错误代码是%d,错误信息是'%s'/n",
- errno, strerror(errno));
- close(new_fd);
- return -1;
- }
- 编译C++代码:
- Need to link lib:
- protobuf
- pthread
- 参考:
- 1,http://www.360doc.com/content/10/0822/16/11586_47942017.shtml
- 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的安装及使用的更多相关文章
- google protobuf安装与使用
google protobuf是一个灵活的.高效的用于序列化数据的协议.相比较XML和JSON格式,protobuf更小.更快.更便捷.google protobuf是跨语言的,并且自带了一个编译器( ...
- 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 ...
- caffe安装编译问题-ImportError: No module named google.protobuf.internal
问题描述 ~/Downloads/caffe$ python Python (default, Dec , ::) [GCC ] on linux2 Type "help", &q ...
- GOOGLE PROTOBUF开发者指南
原文地址:http://www.cppblog.com/liquidx/archive/2009/06/23/88366.html 译者: gashero 目录 1 概览 1.1 什么是pro ...
- google protobuf ios开发使用
简介: protobuf 即 google protocol buffer 是一种数据封装格式协议: 比如其他经常用的xml,json等格式:protobuf的优势是效率高,同样的一份数据使用prot ...
- google protobuf使用
下载的是github上的:https://github.com/google/protobuf If you get the source from github, you need to gener ...
- 在C语言环境下使用google protobuf
本文写给经常使用C编程且不喜欢C++而又要经常使用google protobuf的人. 经常写通讯程序的人对数据进行序列化或者反序列化时,可能经常使用google的protobuf(PB ...
- (原)python中import caffe提示no module named google.protobuf.internal
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5993405.html 之前在一台台式机上在python中使用import caffe时,没有出错.但是 ...
- VS下使用Google Protobuf完成SOCKET通信
如何在Windows环境下的VS中安装使用Google Protobuf完成SOCKET通信 出处:如何在Windows环境下的VS中安装使用Google Protobuf完成SOCKET通信 最近一 ...
随机推荐
- Openfire:安装指南
本文的英文原文来自 http://www.igniterealtime.org/builds/openfire/docs/latest/documentation/install-guide.html ...
- javascript DOM 节点 第18节
<html> <head> <title>DOM对象</title> </head><body><div >DOM对 ...
- Headfirst设计模式的C++实现——工厂方法(Factory Method)
引用原书的一句话:所有的工厂模式都用来封装对象的创建,工厂方法模式通过让子类决定该创建的对象是什么来达到封装的目的. Pizza类及其派生类与上一例相同 PizzaStore.h #ifndef _P ...
- Android从服务端获取json解析显示在客户端上面
Android从服务端获取json解析显示在客户端上面 百度经验:jingyan.baidu.com 首先说一下Json数据的最基本的特点,Json数据是一系列的键值对的集合,和XML数据来比,Jso ...
- 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 ...
- Git---Git及GitHub使用笔记
一.远程项目获取(克隆) syntax: $ git clone <版本库的网址> $ git clone <版本库的网址> <本地目录名> example: $ ...
- linux shell 逻辑运算符
一.逻辑卷标 逻辑卷标 表示意思 1. 关于档案与目录的侦测逻辑卷标! -f 常用!侦测『档案』是否存在 eg: if [ -f filename ] -d 常用!侦测『目录』是否存在 -b 侦测是否 ...
- odoo 清除所有运行数据
测试odoo,如果需要一个干净的db.经常需要清除掉所有业务数据.做如下操作,较为方便 1:建立一个服务器动作,动作的python代码入下. 然后新建一个菜单,菜单动作关联到 这个动作.需要清空db, ...
- share my tools With Xcode
1.让Xcode的控制台支持LLDB类型的打印 在Xcode断点调试的时候, 在控制台输入 po self.view.frame 或者 po id 类型的时候就死翘翘了. 进入正题: 安装LLDB调试 ...
- APP 如何适应 iPhone 5s/6/6Plus 三种屏幕的尺寸
初代iPhone 2007年,初代iPhone发布,屏幕的宽高是 320 x 480 像素.下文也是按照宽度,高度的顺序排列.这个分辨率一直到iPhone 3GS也保持不变. 那时编写iOS的App( ...