Google protobuf的安装及使用
来源:互联网 发布:dnf决战人工智能6.2 编辑:程序博客网 时间:2024/05/29 14:27
最近应为工作的需要,合作的部门提供了protobuf的接口,总结了一下使用的过程和方法如下: 下载protobuf-2.3.0: http://protobuf.googlecode.com/files/protobuf-2.3.0.zip安装: unzip protobuf-2.3.0.zipcd protobuf-2.3.0./configuremake make check make install结果:Libraries have been installed in: /usr/local/libHead 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.protoprotoc -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:protobufpthread参考: 1,http://www.360doc.com/content/10/0822/16/11586_47942017.shtml2,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安装及使用笔记
- linux下Google的Protobuf安装及使用笔记
- Google protobuf的安装及使用和理解
- Google Protobuf的使用
- 【Google Protobuf】protobuf的使用(二)
- Google protobuf 协议的使用
- [protobuf] 如何在Windows环境下的VS中安装使用Google Protobuf完成SOCKET通信
- 【JAVA】google protobuf 3.0 安装使用
- 安装Google protobuf
- Netty与Google的protobuf使用
- Netty 之 Netty使用Google的ProtoBuf
- iOS中ProtocolBuffer2.2的安装及google/protobuf/message.cc:130:60错误解决办法
- Protobuf 的安装使用总结
- protobuf的安装和使用
- protobuf 的安装与使用
- protobuf的安装和使用
- Netty简介
- ios 动画基础
- word分节符
- AR技术在魔术上的应用II
- VS2010,error C2011: “Font”:“struct”类型重定义
- Google protobuf的安装及使用
- linux内核struct链表程序与分析
- 生日相同
- 字符全排列java实现(转)
- Git 命令参数及用法详解 & Linux编程环境下版本控制 Git使用
- How To Choose The Best XML Parser for Your iPhone Project
- Windows 64bit 驱动 和 应用开发 笔记
- VC中的DoDataExchange
- PTIAN EBS FORM开发(二)