天下

记录修行的印记

Google Protocol Buffer 安装和使用

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;
}

posted on 2016-09-22 10:19 天下 阅读(519) 评论(0)  编辑 收藏 引用 所属分类: C/C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2016年9月>
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

导航

统计

常用链接

留言簿(4)

随笔分类(378)

随笔档案(329)

链接

最新随笔

搜索

最新评论