https://www.jianshu.com/p/83fde1089fea
1 源码下载
git clone https://github.com/protocolbuffers/protobuf.git
cd protobuf/
git submodule update --init --recursive
./autogen.sh
2 准备工作
在安装前保证有如下工具,如果没有则自行安装:
autoconf
automake
libtool
make
g++
unzip
On Ubuntu/Debian, you can install them with:
$ sudo apt-get install autoconf automake libtool curl make g++ unzip
3 安装protobuf
接下来执行如下命令,安装时间有点长,耐心等待(可以写成自动化脚本,就无需一步一步执行):
./configure
make
make check
sudo make install
sudo ldconfig # refresh shared library cache.
这时一般来说
libprotobuf库在/usr/local/lib路径下
protoc一般在/usr/local/bin路径下
4 环境配置
下面的环境配置,基于以上两个文件的所在径路,下面命令以本人实际环境配置,具体路径具体更改
$ sudo vim /etc/profile
添加
export PATH=$PATH:/usr/local/bin/
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/
保存执行
source /etc/profile
5 配置动态库路径
sudo vim /etc/ld.so.conf
追加
/usr/local/protobuf/lib
然后以root权限更新动态库路径
ldconfig
./configure -prefix=/usr/local/
sudo make #要编译很久
sudo make check
sudo make install
出错情况一:
configure: error: invalid variable name: `–prefix'
正确格式:把prefix前面的"-"改成“--”
例如:./configure --with-http_stub_status_module "--prefix=/usr/webservice/nginx" --with-pcre=/home/tang/ 下载/pcre-8.20
出错情况二:
我是sudo make时出错了
v2x@ubuntu:~/Desktop/protoBuf/protobuf-3.6.1$ sudo make
make all-recursive
make[1]: Entering directory '/home/v2x/Desktop/protoBuf/protobuf-3.6.1'
Making all in .
make[2]: Entering directory '/home/v2x/Desktop/protoBuf/protobuf-3.6.1'
make[2]: Leaving directory '/home/v2x/Desktop/protoBuf/protobuf-3.6.1'
Making all in src
make[2]: Entering directory '/home/v2x/Desktop/protoBuf/protobuf-3.6.1/src'
depbase=`echo google/protobuf/stubs/bytestream.lo | sed 's|[^/]*$|.deps/&|;s|\.lo$||'`;\
../-libtool --tag=CXX --mode=compile g++ -DHAVE_CONFIG_H -I. -I.. -pthread -DHAVE_PTHREAD=1 -DHAVE_ZLIB=1 -Wall -Wno-sign-compare -O2 -g -std=c++11 -DNDEBUG -MT google/protobuf/stubs/bytestream.lo -MD -MP -MF $depbase.Tpo -c -o google/protobuf/stubs/bytestream.lo google/protobuf/stubs/bytestream.cc &&\
mv -f $depbase.Tpo $depbase.Plo
/bin/bash: line 1: ../-libtool: Permission denied
Makefile:3969: recipe for target 'google/protobuf/stubs/bytestream.lo' failed
make[2]: *** [google/protobuf/stubs/bytestream.lo] Error 126
make[2]: Leaving directory '/home/v2x/Desktop/protoBuf/protobuf-3.6.1/src'
Makefile:1537: recipe for target 'all-recursive' failed
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory '/home/v2x/Desktop/protoBuf/protobuf-3.6.1'
Makefile:1444: recipe for target 'all' failed
问题:
python 使用protobuf出错:protoc: error while loading shared libraries: libprotoc.so.9: cannot open shared object file:No such...
解决方法:linux 敲击命令:export LD_LIBRARY_PATH=/usr/local/lib