protobuf 编译和使用(C语言)

protobuf编译和使用(C语言)


前言

本文主要讲述linux下C语言使用protobuf相关说明


一、什么是Protobuf

XML和JSON是目前常用的数据交换格式,它们直接使用字段名称维护序列化后类实例中字段与数据之间的映射关系,一般用字符串的形式保存在序列化后的字节流中。消息和消息的定义相对独立,可读性较好。但序列化后的数据字节很大,序列化和反序列化的时间较长,数据传输效率不高。
Protobuf是Google提供一个具有高效的协议数据交换格式工具库,类似于JSON,Protobuf和XML、JSON序列化的方式不同,采用了二进制字节的序列化方式,用字段索引和字段类型通过算法计算得到字段之前的关系映射,从而达到更高的时间效率和空间效率,特别适合对数据大小和传输速率比较敏感的场合使用。

二、Protobuf有什么

Protobuf 提供了C++、java、python语言的支持(C需要protobuf-c),提供了windows(proto.exe)和linux平台可执行文件及源文件。Protobuf序列化数据时,常常需要一个proto文件,proto文件定义了协议数据中的数据结构,内容举例如下所示:
syntax = "proto2";  // proto3 必须加此注解
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;
}
  • 实体结构(message): 代表了实体结构,由多个消息字段组成。
  • 消息字段(field): 包括数据类型、字段名、字段规则、字段唯一标识、默认值
  • 数据类型:常见的原子类型都支持(在FieldDescriptor::kTypeToName中有定义)
  • 字段规则:(在FieldDescriptor::kLabelToName中定义)
    required:必须初始化字段,如果没有赋值,在数据序列化时会抛出异常
    optional:可选字段,可以不必初始化。
    repeated:数据可以重复(相当于java 中的Array或List)
    字段唯一标识:序列化和反序列化将会使用到。
  • 默认值:在定义消息字段时可以给出默认值。

三、如何编译

这里linux下C语言环境使用为例介绍。

3.1 protobuf编译安装

  1. 下载地址
    https://github.com/protocolbuffers/protobuf/releases
  2. 确认安装依赖库:automake ,autoconf ,libtool是否已经安装,未安装的需要安装
  3. 下载 protobuf 安装文件,这里下载protobuf-cpp-3.17.1.zip,解压
  4. 编译 (用于生成编译服务器下可执行程序):
./autogen.sh
./configure --prefix=/XX/bin/protobuflib/
make
make install
  1. 交叉编译 (用于交叉编译protobuf-c时用到的依赖库):
./autogen.sh
./configure --host=arm-linux CC=XX CXX=XX --prefix=/XX/bin/protobuflib/
make
make install
  1. 如果你使用的是Java或C++等,完成以上步骤就已经可以使用生成的protobuf可执行程序生产protobuf相关文件了。

3.2 protobuf-c编译安装

由于我的开发环境是linux C语言环境,protobuf仅支持C++,所以这里引入一个其他开源库protobuf-c,解决C语言的支持问题,编译protobuf-c前,需要编译及安装好protobuf。

  1. 下载地址
    https://codechina.csdn.net/mirrors/protobuf-c/protobuf-c/-/releases/v1.4.0?spm=1033.2243.3001.5876
  2. 确认安装依赖库:protobuf, 设置之前protobuf编译出的依赖库资源的环境变量:
PKG_CONFIG_PATH=/XX/bin/protobuflib/
export PKG_CONFIG_PATH
  1. 下载protobuf-c,这里下载protobuf-c-1.4.0.zip解压
  2. 编译 (用于生成编译服务器下可执行程序):
./autogen.sh
./configure --prefix=/XX/bin/protobufc/
make
make install
  1. 交叉编译 (用于实际项目可执行程序的依赖库):
./autogen.sh
./configure --host=arm-linux CC=XX CXX=XX --prefix=/XX/bin/protobufc/  
make
make install
  1. protobufc下生成的文件,即为编译生成的依赖库、protobuf-c可执行程序,使用protobuf-c可执行程序可以根据proto文件生成.c和.h文件

四、如何使用

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

syntax = "proto2";  // proto3 必须加此注解

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

使用如下命令生成student.pb-c.c 和 student.pb-c.h文件。

protoc-c --c_cout=.  student.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      12

static 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 = "2013111011";
    const char *name = "Anker";
    const char *gender = "male";
    const char *object = "computer";
    const char *address = "shen zheng";
    const char *phone = "0102345678";

    strncpy(stu->id, id, ID_LEN);
    strncpy(stu->name, name, NAME_LEN);
    strncpy(stu->gender, gender, GENDER_LEN);
    stu->age = 23;
    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 size
    len = student__get_packed_size(&stu);
    printf("size of student info : %u\n",len);
    buf = malloc(len);
    //put student info pack to buf
    student__pack(&stu, buf);

    //unpack student info from buf
    msg = student__unpack(NULL, len, buf);
    print_student_info(msg);
    //free msg
    student__free_unpacked(msg, NULL);

    free(buf);
    free_student_info(&stu);

    return 0;
}

编译测试:

gcc student.pb-c.c main.c -o main -lprotobuf-c

测试结果:在这里插入图片描述

部分内容摘自以下网址:

http://www.cnblogs.com/dkblog/archive/2012/03/27/2419010.html
https://code.google.com/p/protobuf-c/wiki/Examples
https://www.cnblogs.com/Anker/p/3416541.html

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值