• Linux下protobuf和 protobuf-c安装使用


    如果在 C语言中使用 protobuf,就需要使用 protobuf-c这个库。

    protobuf使用详解:https://blog.csdn.net/qq_42402854/article/details/134066566

    下面在 Linux下安装 protobuf和 protobuf-c。

    一、下载 protobuf和 protobuf-c

    官方的 Protocol Buffer提供了C++、C#、Dart、Go、Java、Kotlin和Python语言的支持。但是不包括C语言。

    1. protoc命令通过 *.proto文件生成支持语言的代码。
    2. protobuf不同语言的库用于代码最终调用时使用

    对于 C语言版本的protobuf-c,只针对C语言做了实现。

    1. protoc-c命令通过 *.proto文件生成对应 C语言的代码(.pb-c.h和.pb-c.c文件),以便在C语言中使用。
    2. libprotobuf-c库用于编译时连接使用。

    protobuf下载地址:https://github.com/protocolbuffers/protobuf/releases
    protobuf-c下载地址:https://github.com/protobuf-c/protobuf-c/releases

    本次下载以 3.19.6版本为例,根据自己需求下载即可。将下载后的压缩包解压到合适的位置。

    [root@centos7 protobuf]# pwd
    /usr/local/protobuf
    # 解压
    [root@centos7 protobuf]# tar -zxvf  protobuf-all-3.19.6.tar.gz
    [root@centos7 protobuf]# tar -zxvf  protobuf-c-1.4.1.tar.gz
    [root@centos7 protobuf]# ll
    总用量 8076
    drwxr-xr-x 18 690013 89939    4096 930 2022 protobuf-3.19.6
    -rw-r--r--  1 root   root  7747455 1017 22:35 protobuf-all-3.19.6.tar.gz
    drwxr-xr-x  8 lisi   lisi      312 711 2022 protobuf-c-1.4.1
    -rw-r--r--  1 root   root   513596 1017 22:35 protobuf-c-1.4.1.tar.gz
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    二、Linux下安装

    1、安装 protobuf

    # 1. 编译安装protobuf
    [root@centos7 protobuf]# cd /usr/local/protobuf/protobuf-3.19.6
    [root@centos7 protobuf-3.19.6]# ./configure --prefix=/usr/local/protobuf/protobuf-3.19.6/
    [root@centos7 protobuf-3.19.6]# make
    [root@centos7 protobuf-3.19.6]# make install
    
    # 2. 添加环境变量
    [root@centos7 protobuf-3.19.6]# cd ~
    [root@centos7 ~]# vim .bashrc
    # 添加这两行
    export PATH="$PATH:/usr/local/protobuf/protobuf-3.19.6/bin"
    export PKG_CONFIG_PATH=/usr/local/protobuf/protobuf-3.19.6/lib/pkgconfig
    # 使之生效
    [root@centos7 ~]# source .bashrc
    
    # 3. 检查是否安装成功,查看版本信息。
    [root@centos7 ~]# protoc --version
    libprotoc 3.19.6
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    编译安装时间有有点长,请耐心等待。

    2、安装 protobuf-c

    # 1. 编译安装protobuf-c
    [root@centos7 ~]# cd /usr/local/protobuf/protobuf-c-1.4.1
    [root@centos7 protobuf-c-1.4.1]# ./configure --prefix=/usr/local/protobuf/protobuf-c-1.4.1
    [root@centos7 protobuf-c-1.4.1]# make
    [root@centos7 protobuf-c-1.4.1]# make install
    
    # 2. 添加环境变量
    [root@centos7 protobuf-c-1.4.1]# cd ~
    [root@centos7 ~]# vim .bashrc
    # 添加这一行
    export PATH="$PATH:/usr/local/protobuf/protobuf-c-1.4.1/bin"
    # 使之生效
    [root@centos7 ~]# source .bashrc
    
    # 3. 检查是否安装成功,查看版本信息。
    [root@centos7 ~]# protoc-c --version
    protobuf-c 1.4.1
    libprotoc 3.19.6
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述

    三、生成 C语言文件

    可以使用 protoc-c命令通过 *.proto文件生成C语言的 .pb-c.h和.pb-c.c文件。

    命令:protoc-c --c_cout=. ./*.proto

    [root@centos7 ~]# mkdir -p /usr/local/protobuf/gen-proto
    [root@centos7 ~]# cd /usr/local/protobuf/gen-proto
    [root@centos7 gen-proto]# vi User.proto
    [root@centos7 gen-proto]# cat User.proto
    syntax = "proto3";
    
    message User {
    
      int32  sex = 1;
      string name = 2;
    
    }
    [root@centos7 gen-proto]# protoc-c --c_out=. ./User.proto
    [root@centos7 gen-proto]# ll
    总用量 12
    -rw-r--r-- 1 root root 2785 1027 23:02 User.pb-c.c
    -rw-r--r-- 1 root root 1947 1027 23:02 User.pb-c.h
    -rw-r--r-- 1 root root   76 1027 23:01 User.proto
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    – 求知若饥,虚心若愚。

  • 相关阅读:
    SIMULIA现实仿真解决方案 SIMULIA仿真模拟应用程序
    B树的定义和特点
    C# 学习第七弹——数组
    Vue中 引入使用 element-resize-detector 监听 Dom 元素 宽度、高度 变化
    vmware中使用Ubuntu,ifconfig没有ens33,ping www.baidu.com出现未知的名称或服务
    [附源码]计算机毕业设计springboot在线票务系统
    2022-11-30 mysql-innodb-ibd文件读取工具
    软件架构设计(业务架构、应用架构、数据架构、技术架构)
    【Linux篇】磁盘lvm管理(PV,VG,LV,PE)
    HHSTU1050型货车转向系及前轴设计(说明书+任务书+CAD图纸)
  • 原文地址:https://blog.csdn.net/qq_42402854/article/details/134085587