当前位置: 首页 > 工具软件 > Ubun-student > 使用案例 >

ubuntu 16.04 下载安装 protobuf C 语言和简单测试

商昂然
2023-12-01

目录

一、安装步骤

二、测试示例

 

一、安装步骤

1> 用 git 登录 github

参考:https://blog.csdn.net/qq_25908839/article/details/103002405  文章目录中的结果

2> 从 github 中下载 protobuf 并编译

以此执行如下命令,来源 https://www.jianshu.com/p/ad986d6e4401

$ git clone https://github.com/protocolbuffers/protobuf.git

$ cd protobuf

$ git submodule update --init --recursive

$ ./autogen.sh

$ ./configure

$ make

$ make check

$ sudo make install

$ sudo ldconfig # refresh shared library cache.

如果遇到如下 RPC failed 的问题

error: RPC failed; curl 18 transfer closed with outstanding read data remaining
 
fatal: The remote end hung up unexpectedly
 
fatal: early EOF
 
fatal: index-pack failed

则试试执行如下命令,然后重启

$ git config --global http.postBuffer 524288000

上述方法百度来的,反正对我不管用

我的方法如下,成功解决!

$ sudo ldconfig

3> 编译 protobuf-c 

返回当前 protobuf 目录的上一级

从 https://github.com/protobuf-c/protobuf-c.git 或用 git 下载 protobuf-c 源码,用 git 下载如下:

$ ssh -T git@github.com 
$ git clone https://github.com/protobuf-c/protobuf-c.git

然后执行

$ cd protobuf-c/
$ ./autogen.sh
$ ./configure
$ sudo make
$ sudo make install

如果无报错,则说明 protobuf-c 编译成功了。

二、测试示例

1> 建立一个 bookInfo.proto 文件

$ vim bookInfo.proto

syntax = "proto2";
package my;
message bookInfo
{
        required string bookName = 1;
        required int32 bookCode = 2;
        optional string publisher = 3;
        required bool borrowState = 4;
        required int32 bookNumber = 5;
}

2> 生成 protobuf .c 文件 和 .h 文件

$ protoc-c --c_out=. bookInfo.proto 
$ ls
bookInfo.pb-c.c  bookInfo.pb-c.h  bookInfo.proto 

.h 文件内容如下

/* Generated by the protocol buffer compiler.  DO NOT EDIT! */
/* Generated from: bookInfo.proto */

#ifndef PROTOBUF_C_bookInfo_2eproto__INCLUDED
#define PROTOBUF_C_bookInfo_2eproto__INCLUDED

#include <protobuf-c/protobuf-c.h>

PROTOBUF_C__BEGIN_DECLS

#if PROTOBUF_C_VERSION_NUMBER < 1000000
# error This file was generated by a newer version of protoc-c which is incompatible with your libprotobuf-c headers. Please update your headers.
#elif 1003002 < PROTOBUF_C_MIN_COMPILER_VERSION
# error This file was generated by an older version of protoc-c which is incompatible with your libprotobuf-c headers. Please regenerate this file with a newer version of protoc-c.
#endif


typedef struct _My__BookInfo My__BookInfo;


/* --- enums --- */


/* --- messages --- */

struct  _My__BookInfo
{
  ProtobufCMessage base;
  char *bookname;
  int32_t bookcode;
  char *publisher;
  protobuf_c_boolean borrowstate;
  int32_t booknumber;
};
#define MY__BOOK_INFO__INIT \
 { PROTOBUF_C_MESSAGE_INIT (&my__book_info__descriptor) \
    , NULL, 0, NULL, 0, 0 }


/* My__BookInfo methods */
void   my__book_info__init
                     (My__BookInfo         *message);
size_t my__book_info__get_packed_size
                     (const My__BookInfo   *message);
size_t my__book_info__pack
                     (const My__BookInfo   *message,
                      uint8_t             *out);
size_t my__book_info__pack_to_buffer
                     (const My__BookInfo   *message,
                      ProtobufCBuffer     *buffer);
My__BookInfo *
       my__book_info__unpack
                     (ProtobufCAllocator  *allocator,
                      size_t               len,
                      const uint8_t       *data);
void   my__book_info__free_unpacked
                     (My__BookInfo *message,
                      ProtobufCAllocator *allocator);
/* --- per-message closures --- */

typedef void (*My__BookInfo_Closure)
                 (const My__BookInfo *message,
                  void *closure_data);

/* --- services --- */


/* --- descriptors --- */

extern const ProtobufCMessageDescriptor my__book_info__descriptor;

PROTOBUF_C__END_DECLS


#endif  /* PROTOBUF_C_bookInfo_2eproto__INCLUDED */

3> 创建一个名为 example.c 文件

/**************************************************************
Author:                     turf
Email:                      ...@qq.com
Date:                       2019-11-07
FileName:                   example.c
Description:                The test script
************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include "bookInfo.pb-c.h"
#include <string.h>

#define BOOK_NAME_LENGTH 40
#define PUBLISHER_LENGTH 40
#define BORROWER_LENGTH 30

int* malloc_book_info(My__BookInfo *bookinfo)
{
        bookinfo->bookname = (char *)malloc(BOOK_NAME_LENGTH);
        if(NULL == bookinfo->bookname)
        {
                return NULL;
        }
        bookinfo->publisher = (char *)malloc(PUBLISHER_LENGTH);
        if(NULL == bookinfo->publisher)
        {
                return NULL;
        }
        
}

static void free_book_info(My__BookInfo *bookinfo)
{
        if(bookinfo->bookname)
        {
                free(bookinfo->bookname);
                bookinfo->bookname = NULL;
        }
        if(bookinfo->publisher)
        {
                free(bookinfo->publisher);
                bookinfo->publisher = NULL;
        }
}

void set_book_info(My__BookInfo *bookinfo)
{
        const char* book_name = "4G Network";
        const int book_code = 20000;
        const char* publisher = "POSTS & TELECOMPRESS";
        const unsigned char borrow_state = 1;
        int book_number = 5;

        strncpy(bookinfo->bookname, book_name, BOOK_NAME_LENGTH);
        bookinfo->bookcode = book_code;
        strncpy(bookinfo->publisher, publisher, PUBLISHER_LENGTH);
        bookinfo->borrowstate = borrow_state;
        bookinfo->booknumber = book_number;
}

void print_book_info(My__BookInfo *bookinfo)
{
        printf("book name: %s\n", bookinfo->bookname);              
        printf("book code: %d\n", bookinfo->bookcode);              
        printf("press: %s\n", bookinfo->publisher);         
        printf("borrow state: %d\n", bookinfo->borrowstate);                
        printf("book number: %d\n", bookinfo->booknumber);          
}
int main()
{
        My__BookInfo bookinfo = MY__BOOK_INFO__INIT;
        void *buf = NULL;
        unsigned int len;
        My__BookInfo *msg = NULL;
        if(NULL == malloc_book_info(&bookinfo))
        {
                printf("malloc falied\n");
                return -1;
        }
        set_book_info(&bookinfo);

        //get student pack size
        len = my__book_info__get_packed_size(&bookinfo);

        printf("sizeof book info = %d\n", len);

        buf = malloc(len);
        //pack book info to buf
        my__book_info__pack(&bookinfo, buf);

        //unpack book info from buf
        msg = my__book_info__unpack(NULL, len, buf);

        print_book_info(msg);
        //free msg
        my__book_info__free_unpacked(msg, NULL);
        free(buf);
        free_book_info(&bookinfo);
        return 0;
}

4> 编译

$ gcc bookInfo.pb-c.c example.c -o a.out -lprotobuf-c

5> 运行

如果遇到如下问题

./a.out: error while loading shared libraries: 
libprotobuf-c.so.1: cannot open shared object file: No such file or directory

则去 https://packages.ubuntu.com/xenial/libs/  找到并下载 libprotobuf-c1 这个 package,安装

$ sudo dpkg -i libprotobuf-c1_1.2.1-1_amd64.deb

安装完毕后再运行

$ ./a.out 
sizeof book info = 42
book name: 4G Network
book code: 20000
press: POSTS & TELECOMPRESS
borrow state: 1
book number: 5

搞掂,收工!

 

 类似资料: