使用protobuf()
来源:互联网 发布:科蓝软件java面试题 编辑:程序博客网 时间:2024/06/07 01:23
Protobuf的简介请看这里:http://blog.csdn.net/program_think/archive/2009/05/31/4229773.aspx
哪我们来讲一下该如何使用
1,首先去谷歌网站上下载(https://github.com/google/protobuf) 我下载的是 2.5版本的
2,编译好工程,如图所示(挨个编译,注: test工程不需要编译),在编译protoc工程的时候, 可能报错,
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall google::protobuf::compiler::CommandLineInterface::CommandLineInterface(void)" (??0CommandLineInterface@compiler@protobuf@google@@QAE@XZ) referenced in function _main1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall google::protobuf::compiler::CommandLineInterface::~CommandLineInterface(void)" (??1CommandLineInterface@compiler@protobuf@google@@QAE@XZ) referenced in function _main1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall google::protobuf::compiler::CommandLineInterface::RegisterGenerator(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class google::protobuf::compiler::CodeGenerator *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?RegisterGenerator@CommandLineInterface@compiler@protobuf@google@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PAVCodeGenerator@234@0@Z) referenced in function _main1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall google::protobuf::compiler::CommandLineInterface::RegisterGenerator(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class google::protobuf::compiler::CodeGenerator *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?RegisterGenerator@CommandLineInterface@compiler@protobuf@google@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0PAVCodeGenerator@234@0@Z) referenced in function _main1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall google::protobuf::compiler::CommandLineInterface::AllowPlugins(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?AllowPlugins@CommandLineInterface@compiler@protobuf@google@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main1>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall google::protobuf::compiler::CommandLineInterface::Run(int,char const * const * const)" (?Run@CommandLineInterface@compiler@protobuf@google@@QAEHHQBQBD@Z) referenced in function _main1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall google::protobuf::compiler::cpp::CppGenerator::CppGenerator(void)" (??0CppGenerator@cpp@compiler@protobuf@google@@QAE@XZ) referenced in function _main1>main.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall google::protobuf::compiler::cpp::CppGenerator::~CppGenerator(void)" (??1CppGenerator@cpp@compiler@protobuf@google@@UAE@XZ) referenced in function _main1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall google::protobuf::compiler::python::Generator::Generator(void)" (??0Generator@python@compiler@protobuf@google@@QAE@XZ) referenced in function _main1>main.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall google::protobuf::compiler::python::Generator::~Generator(void)" (??1Generator@python@compiler@protobuf@google@@UAE@XZ) referenced in function _main1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall google::protobuf::compiler::java::JavaGenerator::JavaGenerator(void)" (??0JavaGenerator@java@compiler@protobuf@google@@QAE@XZ) referenced in function _main1>main.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall google::protobuf::compiler::java::JavaGenerator::~JavaGenerator(void)" (??1JavaGenerator@java@compiler@protobuf@google@@UAE@XZ) referenced in function _main
解决方案:只需要在protoc的工程main文件中添加如下两下:
#pragma comment(lib, "libprotobuf.lib") #pragma comment(lib, "libprotoc.lib")再设置lib的路径: 工程熟属性-》链接-》添加静态库库目录即可(也就是debug目录)
即可,
3,编译好之后,在工程libprotobuf中生成debug文件,libprotobuf.lib,protoc.exe三个文件
4,工程libprotobuf中目录下,运行extract_includes.bat,产生include文件夹
5,新建一个工程 testProtoBuf, 将libprotobuf.lib,protoc.exe,include文件夹,这三个文件复制到新建工程的跟目录下,如图所示
5,配置新项目环境,如图所示
6,配置好环境,我们需要在跟目录下创建如下两个文件
7,运行build.bat文件,在上层目录产生两个文件person.pb.h,person.pb.cc,然后将其放到
8,在工程中新建一个cpp文件,如图所示:
代码如下:
#include <iostream>#include <fstream>#include <string>#include "person.pb.h"#pragma comment (lib,"libprotobuf.lib")using namespace std;int main(){Person person;person.set_id(6);person.set_email("adc");person.set_name("gan");/*文件序列化*/fstream out("person.xml", ios::out | ios::binary | ios::trunc);person.SerializeToOstream(&out);out.close();//从person.pb文件读取数据fstream in("person.xml", ios::in | ios::binary);if (!person.ParseFromIstream(&in)) {cerr << "Failed to parse person.xml." << endl;exit(1);}cout << "ID: " << person.id() << endl;cout << "name: " << person.name() << endl;if (person.has_email()) {cout << "e-mail: " << person.email() << endl;}return 0;}
9,进行编译时,可能会出现一个错误
10.在工程属性中添加如下即可:_SCL_SECURE_NO_WARNINGS
0 0
- 使用protobuf()
- protobuf使用
- protobuf 使用
- protobuf使用
- protobuf使用
- 使用protoBuf
- protobuf-c的使用(二)使用
- 【protobuf】protobuf ubuntu python3使用
- protobuf的使用误区(cpp)
- protobuf 在lua中使用(3)
- [Unity3D]简单使用Protobuf-net(一)
- [Unity3D]简单使用Protobuf-net(二)
- [Unity3D]简单使用Protobuf-net(三)
- protobuf-c的使用(一)构建
- [Unity3D]简单使用Protobuf-net(一)
- [Unity3D]简单使用Protobuf-net(二)
- [Unity3D]简单使用Protobuf-net(三)
- protobuf安装使用(python版)
- scanf()函数
- Oracle常用的数据操作函数
- Unity3D之实现火炬之光遮挡效果
- 使用TFTP进行文件传输
- 文章标题
- 使用protobuf()
- css hack大全
- 1、Android概述
- linux下使用inode删除乱码或无法识别文件
- leetcode之Search in Rotated Sorted Array
- Python2.*脚本转换成Python3.*脚本
- 正确使用snprintf函数
- windows7 搭建 Spark 详细图解
- json