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

二、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

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

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

在这里插入图片描述

三、生成 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

– 求知若饥,虚心若愚。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值