Google Protocol Buffer 安装和使用
下载:
https://codeload.github.com/google/protobuf/
看README.md
For non-C++ users, the simplest way to install the protocol compiler is to
download a pre-built binary from our release page:
[https://github.com/google/protobuf/releases]
下载C++的版本:
https://github.com/google/protobuf/releases
进入:protobuf-3.0.2\cmake 目录
用cmake 处理编译
编译默认的代码生成是MT,不是MD
只需要2个工程:
libprotobuf和protoc
编译完成之后:把include目录拷贝出来
新建SearchRequest.proto文件
message SearchRequest
{
required string query = 1;
optional int32 page_number = 2;// Which page number do we want?
optional int32 result_per_page = 3;// Number of results to return per page.
}
protoc.exe --cpp_out ./ SearchRequest.poto
#include "stdafx.h"
#include "SearchRequest.pb.h"
//#pragma comment(lib,"../Debug/MT/libprotobufd.lib")
#pragma comment(lib,"../Debug/MD/libprotobufd.lib")
int _tmain(int argc, _TCHAR* argv[])
{
SearchRequest sr;
sr.set_query("abcd",5);
sr.set_page_number(0x1001);
sr.set_result_per_page(0x1002);
std::string data = sr.SerializeAsString();
SearchRequest sr2;
sr2.ParseFromString(data);
return 0;
}