Protobuf的简介请看这里:
哪我们来讲一下该如何使用
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 _main
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall google::protobuf::compiler::CommandLineInterface::~CommandLineInterface(void)" (??1CommandLineInterface@compiler@protobuf@google@@QAE@XZ) referenced in function _main
1>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 _main
1>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 _main
1>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 _main
1>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 _main
1>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 _main
1>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 _main
1>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 _main
1>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 _main
1>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 _main
1>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