您的位置:首页 > 其它

安装protobuf可能遇到的问题

2013-08-27 10:53 381 查看
下载protobuf-2.3.0:

http://protobuf.googlecode.com/files/protobuf-2.3.0.zip
http://code.google.com/p/protobuf/downloads/list
安装:

unzip protobuf-2.3.0.zip

cd protobuf-2.3.0

./configure

make

make check

make install

1.每步在执行过程中注意权限问题,有的命令不sudo执行会出问题。

./configure

make

make check

make install

2. 执行./configure 出现error,查看config.log文件看看是否缺某些安装软件,比如g++未安装等,因为执行文件中含有C++程序,所以出问题。

3.动静态库问题部分是转载

在Linux上编译google protobuff时,configure 默认选项是生成动态库,即libprotobuf.so文件。如果同时在多个动态库(动态库以dlopen方式动态加载)中使用同一buff结构,则运行时会报错误:

ibprotobuf ERROR google/protobuf/descriptor_database.cc:57] File already exists in database: foo/foo.proto libprotobuf FATAL google/protobuf/descriptor.cc:862] CHECK failed: generated_database_->Add(encoded_file_descriptor, size):  terminate called after throwing an instance of 'google::protobuf::FatalException'   what():  CHECK failed: generated_database_->Add(encoded_file_descriptor, size):

为了解决这个问题,google protobuff,则不能以动态库的形式调用,改用静态库的形式在编译时加载。
编译google protobuff时,在configure 时加上选项:
configrue --disable-shared

即可编译成静态库:libprotobuf.a 但是默认的configure文件中,在编译时未加-fPIC ,导致在引用静态库的工程中编译链接时报错误:

libs/assert.o: relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC .libs/assert.o: could not read symbols: Bad value
解决该问题,需要重新编译google protobuff库,并添加编译选项:-fPIC
以文本形式打开google buff代码目录下的configure文件,在把第2575至2578行修改为如下:
if test "x${ac_cv_env_CFLAGS_set}" = "x"; then :   CFLAGS="-fPIC" fi if test "x${ac_cv_env_CXXFLAGS_set}" = "x"; then :   CXXFLAGS="-fPIC"

需要注意的是不同版本的configure文件不同,所以源代码的行数也不同,2.3.0是1962行开始,贴出被替换代码,以便于替换

if test "x${ac_cv_env_CFLAGS_set}" = "x"; then

CFLAGS=""

fi

if test "x${ac_cv_env_CXXFLAGS_set}" = "x"; then

CXXFLAGS=""

fi

替换时注意if 和fi 的配对使用,否则执行不了,会出现语法错误,文件无法正常结束。

4.在修改文件后编译要重新编译,首先进行make clean ,否则不会重新执行。

5.最后修改环境变量,建议修改本用户的环境变量,~/.bashrc,不修改etc下环境变量
# append protobuf to PATH/lys

export PROTOBUF_HOME=/usr/local/protobuf/protobuf-2.3.0

export PATH=$PROTOBUF_HOME/bin:$PATH

HOME目录由安装目录而定,各不相同。

6.测试是否安装成功,protoc --version,显示出版本则说明安装成功
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: