protobuf简单序列化反序列化示例

protoc命令格式 

protoc    -I=SRC -cpp_out = DRC SRC\*.proto 

SRC:源路径;

DRC:目的路径;

 

当出现下面无法打开文件错误时,应在工程属性目录下的包含目录文件中添加工程所在路径 D:\!exercise\c++\protob1\protob1\

错误 1 error C1083: 无法打开包括文件:“google/protobuf/stubs/common.h”: No such file or directory d:\!exercise\c++\protob1\protob1\google\protobuf\generated_message_util.h 43 1 protob1

 

当现现下面无法打开库文件时,应用工程属性目录下的库目录添加LIB文件所在路径:D:\!software\!develop\protobuf-2.5.0\protobuf-2.5.0\vsprojects\Debug

错误 3 error LNK1104: 无法打开文件“libprotoc.lib” D:\!exercise\c++\protob1\protob1\LINK protob1

附:源代码

 1 #include "iostream"
 2 #include "person.pb.h"
 3 #include "fstream"
 4 
 5 using namespace std;
 6 #pragma comment(lib,"libprotoc.lib")
 7 #pragma comment(lib,"libprotobuf.lib")
 8 #pragma comment(lib,"libprotobuf-lite.lib")
 9 int main()
10 {
11     lm::helloworld hw;
12     hw.set_id(11);
13     hw.set_str("str");
14     
15     cout<<"id="<<hw.id()<<endl;
16     cout <<"str"<<hw.str()<<endl;
17 
18      std::fstream out( "person.pb", std::ios::out | std::ios::binary | std::ios::trunc );
19     hw.SerializeToOstream( &out );
20     out.close();
21 
22     lm::helloworld hw1;
23     std::fstream in( "person.pb", std::ios::in | std::ios::binary );
24     if ( !hw1.ParseFromIstream( &in ) ) {
25       std::cerr << "Failed to parse person.pb." << std::endl;
26       exit(1);
27     }
28 
29     std::cout << "ID: " << hw1.id() << std::endl;
30     std::cout << "name: " << hw1.str() << std::endl;
31   
32     google::protobuf::ShutdownProtobufLibrary();
33 
34     system("pause");
35     return 0;
36 }

 

posted on 2015-04-05 21:57  dingyun  阅读(1293)  评论(0编辑  收藏  举报

导航