Protoc安装

1、下载Protocol Buffers的安装包: https://github.com/google/protobuf  和https://github.com/protobuf-c/protobuf-c

2、安装步骤:
      安装需要的依赖包:
      [root@hanbo]# apt-get install autoconf automake libtool curl make g++ unzip
      [root@hanbo]# unzip protobuf-master.zip
      [root@hanbo]# cd protobuf-master
      生成configure文件的脚本文件,如果不执行这步,以下操作将通不过
      [root@hanbo protobuf-master]#  ./autogen.sh
      [root@hanbo protobuf-master]#  ./configure
      可以修改安装目录通过 ./configure --prefix=命令,统一安装在/usr/local/protobuf下   (一般不要修改,配置不完整会影响使用)
      [root@hanbo protobuf-master]# ./configure --prefix=/usr/local/protobuf
      [root@hanbo protobuf-master]#  make
      [root@hanbo protobuf-master]#  make check
      [root@hanbo protobuf-master]#  make install
      [root@hanbo protobuf-master]#  ldconfig # refresh shared library cache.

一、简介

Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,很适合做数据存储或 RPC 数据交换格式。

它可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。目前提供了 C++、Java、Python 三种

语言的 API。

二、proto文件格式

message AMessage
{requried  int32  a = 1;  //a必须出现optional  string b = 2;  //b是可选的repeated  int32  c = 3;  //c是数组
}

字段规则类型:

required:表示后面的数据是必须的。

optional:表示后面数据是可选的。

repeated:表示后面的数据是一个数组。

三、测试程序

编写一个学生信息的proto,proto文件内容如下所示:

message Student
{required string id = 1;required string name = 2;required string gender = 3;required int32  age = 4;required string object = 5;required string home_address = 6;required string phone = 7;
}

编译命令: protoc-c --c_out=.  student.proto

生成student.pb-c.c 和 student.pb-c.h两个文件。student.pb-c.h文件内容如下所示:

/* Generated by the protocol buffer compiler.  DO NOT EDIT! */#ifndef PROTOBUF_C_student_2eproto__INCLUDED
#define PROTOBUF_C_student_2eproto__INCLUDED#include <google/protobuf-c/protobuf-c.h>PROTOBUF_C_BEGIN_DECLStypedef struct _Student Student;/* --- enums --- */
/* --- messages --- */struct  _Student
{ProtobufCMessage base;char *id;char *name;char *gender;int32_t age;char *object;char *home_address;char *phone;
};
#define STUDENT__INIT \{ PROTOBUF_C_MESSAGE_INIT (&student__descriptor) \, NULL, NULL, NULL, 0, NULL, NULL, NULL }/* Student methods */
void   student__init(Student         *message);
size_t student__get_packed_size(const Student   *message);
size_t student__pack(const Student   *message,uint8_t             *out);
size_t student__pack_to_buffer(const Student   *message,ProtobufCBuffer     *buffer);
Student *student__unpack(ProtobufCAllocator  *allocator,size_t               len,const uint8_t       *data);
void   student__free_unpacked(Student *message,ProtobufCAllocator *allocator);
/* --- per-message closures --- */typedef void (*Student_Closure)(const Student *message,void *closure_data);/* --- services --- */
/* --- descriptors --- */extern const ProtobufCMessageDescriptor student__descriptor;PROTOBUF_C_END_DECLS#endif  /* PROTOBUF_student_2eproto__INCLUDED */

测试proto程序如下所示:

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "student.pb-c.h"#define ID_LEN         11
#define NAME_LEN       32
#define GENDER_LEN     10
#define OBJECT_LEN     20
#define HOME_ADDR_LEN  96
#define PHONE_LEN      12static int malloc_student_info(Student *stu)
{stu->id = (char*)malloc(ID_LEN);if (!stu->id){goto FAILED;}stu->name = (char*)malloc(NAME_LEN);if (!stu->name){goto FAILED;}stu->gender = (char*)malloc(GENDER_LEN);if (!stu->gender){goto FAILED;}stu->object = (char*)malloc(OBJECT_LEN);if (!stu->object){goto FAILED;}stu->home_address = (char*)malloc(HOME_ADDR_LEN);if (!stu->home_address){goto FAILED;}stu->phone = (char*)malloc(PHONE_LEN);if (!stu->phone){goto FAILED;}return 0;
FAILED:fprintf(stdout, "malloc error.errno:%u,reason:%s\n",errno, strerror(errno));return -1;
}static void free_student_info(Student *stu)
{if (stu->id){free(stu->id);stu->id = NULL;}if (stu->name){free(stu->name);stu->name = NULL;}if (stu->gender){free(stu->gender);stu->gender = NULL;}if (stu->object){free(stu->object);stu->object = NULL;}if (stu->home_address){free(stu->home_address);stu->home_address = NULL;}if (stu->phone){free(stu->phone);stu->phone = NULL;}
}static void set_student_info(Student *stu)
{const char *id = "2016041117";const char *name = "Hslong";const char *gender = "male";const char *object = "computer";const char *address = "han dan";const char *phone = "03111234567";strncpy(stu->id, id, ID_LEN);strncpy(stu->name, name, NAME_LEN);strncpy(stu->gender, gender, GENDER_LEN);stu->age = 25;strncpy(stu->object, object, OBJECT_LEN);strncpy(stu->home_address, address, HOME_ADDR_LEN);strncpy(stu->phone, phone, PHONE_LEN);
}void print_student_info(Student *stu)
{printf("id: %s\n",stu->id);printf("name: %s\n",stu->name);printf("age: %d\n",stu->age);printf("gender:%s\n",stu->gender);printf("object: %s\n",stu->object);printf("home address: %s\n",stu->home_address);printf("phone: %s\n",stu->phone);
}int main()
{Student stu = STUDENT__INIT;void *buf = NULL;unsigned int len ;Student *msg = NULL;if (malloc_student_info(&stu) == -1) {exit(0);}   set_student_info(&stu);//get student packed sizelen = student__get_packed_size(&stu);printf("size of student info : %u\n",len);buf = malloc(len);//put student info pack to bufstudent__pack(&stu, buf);//unpack student info from bufmsg = student__unpack(NULL, len, buf);print_student_info(msg);//free msgstudent__free_unpacked(msg, NULL);free(buf);free_student_info(&stu);return 0;
}

编译命令: gcc student.pb-c.c main.c -o main -lprotobuf-c

测试结果如下所示:




protobuf之c基础相关推荐

  1. ProtoBuf的序列化和反序列化(基础库)

    内容说明: 说明下什么是ProtoBuf ProtoBuf的序列化和反序列化 ProtoBuf的优势 前后端使用ProtoBuf交互 ProtoBuf简介: Google 的 ProtoBuf ==& ...

  2. grpc java 基础教程

    1 RPC 框架原理 RPC 框架的目标就是让远程服务调用更加简单.透明,RPC 框架负责屏蔽底层的传输方式(TCP 或者 UDP).序列化方式(XML/Json/ 二进制)和通信细节.服务调用者可以 ...

  3. 数据上报痛点解决方案

    作者:donnyhuang,腾讯微信支付运营支持研发团队 数据驱动是近年来很火的概念,可以优化产品体验.可以用于运营增长.可以发现质量问题,看起来无所不能,时尚先进.但是,你得先有"数据上报 ...

  4. 带你快速了解 Docker 和 Kubernetes

    作者:honghaohu,腾讯 PCG 后台开发工程师 从单机容器化技术 Docker 到分布式容器化架构方案 Kubernetes,当今容器化技术发展盛行.本文面向小白读者,旨在快速带领读者了解 D ...

  5. 360手机卫士插件化RePlugin今日开源,官方全面解读

    作者:张炅轩,360手机卫士·客户端技术专家 写在前面 "RePlugin将在6月底开源,这将是我们献给安卓世界最好的礼物."当我们宣布这一消息时,心中的激动,无以言表.是的,三年 ...

  6. 负载、集群功能Beetle Agent简介

    Beetle Agent是基于Beetle开发的代理服务软件,其主要目的是接收用户请求并根据实际请求的信息进行一个负载处理,它可以灵活地把请求分发到不同的应用服务器并把应用服务器处理的结果返回给具体的 ...

  7. gRPC基础--Protobuf编码格式详解

    什么是 Protobuf Protobuf是Protocol Buffers的简称,它是Google公司开发的一种数据描述语言,用于描述一种轻便高效的结构化数据存储格式,并于2008年对外开源.Pro ...

  8. 【Java从0到架构师】分布式框架通信核心基础 - 序列化(JDK、Protobuf)、远程过程调用 RMI

    分布式框架通信核心基础 序列化 JDK 的序列化 JDK 序列化的一些细节 Protobuf 序列化 Protobuf 环境搭建与操作 Protobuf 原理分析 实际数据传输 序列化技术选型 远程过 ...

  9. 《Unity 3D游戏客户端基础框架》protobuf 导excel表格数据

    前言: 之前使用NPOI插件编写的导表工具,其实就是直接将数据进行序列化,解析时还需要进行反序列化,步骤比较繁复,最近看到Google的一个开源的项目protobuf,不仅可以用于进行excel表格数 ...

最新文章

  1. Codeforces Round #726 (Div. 2) F. Figure Fixing 二分图 + 思维
  2. Win7系统电脑修改不了文件属性怎么办
  3. 美团智能搜索推荐模型预估框架的建设与实践
  4. 空心点_空心水泥砖的多种花园用途,总有一款GET你的点!
  5. C#的变迁史 - C# 4.0 之多线程篇
  6. 记element + xlsx 导出表格数据重复的坑
  7. 社交网络图中结点的“重要性”计算 (30 分)(Floyd)
  8. 不同手机型号图文预览_手机支持型号汇总
  9. 基于Servlet和jsp的小说网站系统
  10. 西部数据移动硬盘哪个型号好_西部数据移动硬盘怎么样(西数移动硬盘系列区别)...
  11. onenote 实现不同端 秒同步
  12. 【IT之路】连接MySQL遇到ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using passwor:yes)问题
  13. 第二章 03 藤蔓生长
  14. Render函数渲染页面
  15. AI萃取的5G咖啡,只有华为能调出这个味道
  16. Google浏览器密码框自动弹出账号密码的解决方法
  17. 电脑屏幕设置(亮度,防蓝光...)-台式机显示器
  18. 量子计算核心突破!Shor算法实现或使密码成摆设
  19. 圆周角、圆心角、弦、弦心距、弧长、扇形面积
  20. U-BOOT分析(二)之顶层Makefile文件(1)

热门文章

  1. 给 Word 文档中的公式自动编号
  2. centos7/8--开机启动流程(附图片)
  3. Python 中的 urlencode 和 urldecode 操作
  4. 文章内容页调用缩略图
  5. 《领域驱动设计》阅读笔记 第1章 消化知识
  6. android 沉浸式状态栏 19,Android 沉浸式状态栏 以及 伪沉浸式状态栏
  7. sdcard sdhci 中 L11 和 L12 长开的方法:
  8. Oracle用户权限赋予
  9. python怎么暂停运行_如何暂停长时间运行的循环?
  10. YTU 2901: G-险恶逃生II