protobuf在使用的时候使用静态链接库方式

转自 https://blog.csdn.net/dreamvyps/article/details/73224627

protobuf默认安装的时候,configure使用的是使用动态链接库的方式进行安装和使用的,在使用过程中,会报出这个错误:

[libprotobuf ERROR google/protobuf/descriptor_database.cc:57] File already exists in database: pro_projectasset.proto
[libprotobuf FATAL google/protobuf/descriptor.cc:1164] 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): 


当时自己并没有搞清楚问题的根源。在网上看到是可以通过将protobuf链接的时候改成静态链接库的方式进行。后续跟进

这里先记录一下问题的解决方案:

即是对protobuf重新编绎安装,步骤如下:

编译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:
configure --disable-shared
make 
make install
编译完成后,使用libprotobuf.a文件

猜你喜欢

转载自blog.csdn.net/CareyTian/article/details/80353314
今日推荐