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 _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