google proto buffer安装和简单示例

简介:

1、安装

下载google proto buff

解压下载的包,并且阅读README.txt,根据里面的指引进行安装。

$ ./configure

$ make

$ make check

$ make install

没有意外的话,前面三步应该都能顺利完成,第四步的时候,需要root权限。我采用的默认的路径,所以,仅仅用root权限,还是安装不了,要自己先在/usr/local下新建一个lib的目录,然后执行make install,这样,应该就能顺利安装google proto buffer了。

安装完后,先写一个测试程序来测试下安装,先来看看proto文件:

package hello;

message Hello

{

required int32 id = 1; //user id

required string name = 2; //user name

optional string email = 3; //user email

}

接着,要用protoc生成一个对应的类,我把它生成在./out目录里:

protoc hello.proto --cpp_out=./out

接下来,在out目录下,会生成两个文件:

$> ls

hello.pb.cc hello.pb.h

接下来,编写测试用的c++代码:

hello.cc

#include <stdio.h>

#include <string.h>

#include "out/hello.pb.h"

using namespace std;

using namespace hello;

int main()

{

Hello a;

a.set_id(101);

a.set_name("xg");

string tmp;

bool ret = a.SerializeToString(&tmp);

if (ret)

{

printf("encode success!\n");

}

else

{

printf("encode faild!\n");

}

Hello b;

ret = b.ParseFromString(tmp);

if (ret)

{

printf("decode success!\n id= %d \n name = %s\n", b.id(), b.name().c_str());

}

else

{

printf("decode faild!\n");

}

return 0;

}

接着,编译一下这个代码,由于使用了protobuf的库,所以编译的时候,要把这些库也链接进来:

g++ hello.cc ./out/hello.pb.cc -o hello -I./out -I/usr/local/protobuf/include -L/usr/local/lib -lprotobuf

这样,就生成了测试程序。

运行一下:

$> ./hello

encode success!

decode success!

id= 101

name = xg

本文转自博客园知识天地的博客,原文链接:google proto buffer安装和简单示例,如需转载请自行联系原博主。


相关文章
|
8月前
|
Web App开发
kali2022.1安装google chrome develop 专业版
kali2022.1安装google chrome develop 专业版
59 1
|
7月前
|
Web App开发 Ubuntu 安全
【已解决】ubuntu 16.04安装最新版本google chrome出错, 旧版本chrome浏览器安装流程
【已解决】ubuntu 16.04安装最新版本google chrome出错, 旧版本chrome浏览器安装流程
510 2
|
6月前
解决kali安装不了Google输入法的原因
解决kali安装不了Google输入法的原因
43 0
|
11月前
|
Shell
(Mac)remotedebug-ios-webkit-adapter 无法运行,出现以下错误:ios_webkit_debug_proxy找不到。请安装 ios_webkit_debug_proxy (https://github.com/google/ios-webkit-debug-proxy) #74
(Mac)remotedebug-ios-webkit-adapter 无法运行,出现以下错误:ios_webkit_debug_proxy找不到。请安装 ios_webkit_debug_proxy (https://github.com/google/ios-webkit-debug-proxy) #74
447 0
|
存储 编译器 API
Google Protocol Buffer Basics: C++
Google Protocol Buffer Basics: C++
|
存储 JSON Java
Google Protocol Buffer
Google Protocol Buffer
124 0
|
测试技术 编译器 开发工具
C++服务性能优化的道与术-道篇:google benchmark的安装与使用
如果你实现一个公共的工具函数,有多种实现方式,你怎么测试性能呢?是循环多少次,然后打印一下起止时间,计算耗时吗?这样当然没问题。但是每次都类似的需求,都会写很多冗余的代码来进行耗时统计,另外也缺乏灵活性。有没有方便的方式来测试呢?有,Google家的benchmark性能测试框架。
1000 2
C++服务性能优化的道与术-道篇:google benchmark的安装与使用
|
JSON JavaScript 前端开发
Google Earth Engine(GEE)——客户端python的安装与使用
Google Earth Engine(GEE)——客户端python的安装与使用
1112 0
|
Java Android开发 开发者
【Google Play】从 Android 应用中跳转到 Google Play 中 ( 跳转代码示例 | Google Play 页面的链接格式 | Google Play 免安装体验 )
【Google Play】从 Android 应用中跳转到 Google Play 中 ( 跳转代码示例 | Google Play 页面的链接格式 | Google Play 免安装体验 )
591 0
【Google Play】APK 扩展包 ( 2021年09月02日最新处理方案 | 内部测试链接 | 安装 Google Play 中带 扩展文件 的 APK 安装包 | 验证下载的扩展文件 )(二)
【Google Play】APK 扩展包 ( 2021年09月02日最新处理方案 | 内部测试链接 | 安装 Google Play 中带 扩展文件 的 APK 安装包 | 验证下载的扩展文件 )(二)
139 0
【Google Play】APK 扩展包 ( 2021年09月02日最新处理方案 | 内部测试链接 | 安装 Google Play 中带 扩展文件 的 APK 安装包 | 验证下载的扩展文件 )(二)

热门文章

最新文章