一、概述
Protocol Buffers (a.k.a., protobuf) are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data for use in communications protocols, data storage, and more.
参考官网:https://developers.google.com/protocol-buffers/
二、安装
2.1 系统默认protobuf
Ubuntu 16.04安装了protobuf 2.6.1。
查看protobuf的版本的命令:
protoc --version
查看protoc路径:
which protoc
系统默认安装路径/usr/bin/protoc
2.2 安装指定版本的protobuf
各版本protobuf下载地址: https://github.com/protocolbuffers/protobuf/releases/ ,根据需要选择版本分支,以3.6.1为例。
安装方法一:
若后续使用protobuf时需要经常使用cmakelist编译工程,推荐编译Protobuf时也使用cmakelist :
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=/home/yly/Software/protobuf-3.6.1/local ../cmake/ #-DCMAKE_INSTALL_PREFIX中指定的路径为make install安装的路径. protobuf工程的Cmakelist在cmake子文件夹下
make -j12 #编译
make install #从makefile中读取指令,将inclue、lib、 bin安装(拷贝到)到指定位置-DCMAKE_INSTALL_PREFIX的子目录中
PS: 建议:编译前将cmake文件夹下的cmakelist.txt中添加add_compile_options(-fPIC),否则后续有些工程链接库时会报错:
/usr/bin/ld: /home/yly/Software/protobuf-3.6.1/local/lib/libprotobuf.a(arena.cc.o): relocation R_X86_64_32 against `_ZN6google8protobuf8internal9ArenaImpl23lifecycle_id_generator_E' can not be used when making a shared object; recompile with -fPIC
安装方法二:
首先生成configure:
./autogen.sh
PS: 如果出现如下错误 ./autogen.sh: 37: ./autogen.sh: autoreconf: not found或者autoreconf: /usr/bin/autoconf failed with exit status: 1错误,需要先安装autoconf,automake和libtool
sudo apt-get update #autoconf和automake是用来发布C程序 sudo apt-get install autoconf sudo apt-get install automake sudo apt-get install libtool
随后,从makefi