C++ protobuf使用方法
来源:互联网 发布:移动的网络制式 编辑:程序博客网 时间:2024/06/09 23:17
转自:http://www.cppblog.com/jack-wang/archive/2013/11/22/204378.aspx
下载protobuf-2.5.0(解压到D:\下)
编译,在目录D:\protobuf-2.5.0\vsprojects\Debug之下,生成若干.lib,.exe程序(好乱,中间文件和生成文件都放到一个目录下了,眼都看花了)另外单独建立新项目test
将protobuf-2.5.0中生成的libprotobuf.lib、libprotoc.lib、protoc.exe复制到新项目Test的工作目录下
在新项目test工作目录下新建文本文件test.proto
编辑文件test.proto内容如下:
package Test;
message Person
{
required string name = 1;
required int32 id = 2;
optional string email = 3;
}
在控制台中输入命令
protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/test.proto
(翻译一下:protoc -I=D:\test\test --cpp_out=D:\test\test D:\test\test\person.proto)
生成文件:
person.pb.cc
person.pb.h
将这两个文件加入到新项目test中,编译新项目
o了!
附:新项目中使用protobuf自动生成的c++文件的相关代码
#include "stdafx.h"
#include "person.pb.h"
#include <iostream>
#include <fstream>
int _tmain(int argc, _TCHAR* argv[])
{
// Verify that the version of the library that we linked against is
// compatible with the version of the headers we compiled against.
GOOGLE_PROTOBUF_VERIFY_VERSION;
// 设置数据, 并序列化到文件
Test::Person person;
person.set_id( 123 );
person.set_name( "abc" );
person.set_email( "abc@163.com" );
std::fstream out( "person.pb", std::ios::out | std::ios::binary | std::ios::trunc );
person.SerializeToOstream( &out );
out.close();
// 从文件中读取数据, 并且反序列化
Test::Person person1;
std::fstream in( "person.pb", std::ios::in | std::ios::binary );
if ( !person1.ParseFromIstream( &in ) ) {
std::cerr << "Failed to parse person.pb." << std::endl;
exit(1);
}
std::cout << "ID: " << person1.id() << std::endl;
std::cout << "name: " << person1.name() << std::endl;
if ( person1.has_email() ) {
std::cout << "e-mail: " << person1.email() << std::endl;
}
// Optional: Delete all global objects allocated by libprotobuf.
google::protobuf::ShutdownProtobufLibrary();
getchar();
return 0;
}
- protobuf使用方法
- protobuf使用方法
- protobuf-c
- protobuf 嵌套协议使用方法
- C# protobuf的使用方法
- C# protobuf的使用方法
- Protobuf的使用方法
- C++ protobuf使用方法
- C# protobuf的使用方法
- Unity中protobuf的使用方法
- protobuf使用方法(c++为例)
- Unity中protobuf的使用方法
- Unity中protobuf的使用方法
- protobuf-c学习笔记
- protobuf-c学习总结
- protobuf-c学习小结
- protobuf之c基础
- C++-glog/gflags/protobuf
- Java中堆内存(heap)和栈内存(stack)的区别
- RocketMQ最佳实践(四)可视化管理控制台rocketmq-console-ng
- 在Android Studio中指定import的先后顺序
- [置顶] Java的静态代理、动态代理,CGLib的动态代理,使用动态代理基于AOP的AspectJ框架—深入探究
- target_compile_options
- C++ protobuf使用方法
- 大乱炖
- Git , TortoiseGit 操作记住密码
- 手工写一个分词器
- 类别
- 座右铭
- 求最大公约数和最小公倍数
- android 6.0 权限管理
- 【python 语法】继承