protobuf支持一些主流的语言,唯独没有支持C。所以诞生了第三方的protobuf-c。
https://github.com/protocolbuffers/protobuf
https://github.com/protobuf-c/protobuf-c
国内镜像仓库:
https://gitee.com/mirrors/protobuf_source
https://gitee.com/mirrors/protobuf-c
在安装 protobuf-c 之前需要安装 protobuf
1、安装protobuf
安装protobuf需要依赖一些工具,需要先安装依赖:
sudo apt-get install autoconf automake libtool curl make g++ unzip
安装完依赖后一依次输入如下命令下载、编译、安装(下载速度慢的话可以先导入码云再下载)
git clone https://github.com/protocolbuffers/protobuf.git
cd protobuf
git submodule update --init --recursive
./autogen.sh
./configure
make
sudo make install
sudo ldconfig
其中,执行./autogen.sh命令为了生成configure配置脚本,执行configure可生成Makefile文件,执行make进行编译,执行sudo make install命令进行安装,执行sudo ldconfig命令让动态链接库为系统所共享。
碰到的问题记录:
1、运行./autogen.sh的时候下载gtest失败
原因分析:
通过分析autogen.sh文件,可以看到会从googletest.googlecode.com下载gtest。
curl http://googletest.googlecode.com/files/gtest-1.5.0.tar.bz2 | tar jx
mv gtest-1.5.0 gtest
解决方法:
手动从国内码云仓库下载对应版本,文件夹重命名后,放到指定位置
2、安装protobuf-c
同样的,protobuf-c也要依赖于 pkg-config ,输入以下命令进行安装:
sudo apt-get install pkg-config
然后输入如下命令下载、编译、安装protobuf-c:
git clone https://github.com/protobuf-c/protobuf-c.git
cd protobuf-c
./autogen.sh
./configure
make
sudo make install
按以上方式安装的话,protobuf与protobuf-c默认安装在/usr/local路径下:
查看版本:
生成的.h头文件中有如下校验:
如果想要更新protobug的版本,需要按照上述编译安装的方式重新走一遍。还需要重新编译安装一遍protobuf-c的版本,否则:protobuf-c查到的lib还是老的!
ref:
protoc- c 踩坑使用_zb313982521的博客-CSDN博客
protobuf c语言版本_bubuxindong的博客-CSDN博客