• Linux 下使用静态google protocl buffer


    在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:
    configrue --disable-shared
    make 
    make install
    编译完成后,使用libprotobuf.a文件,以上问题解决。
  • 相关阅读:
    SSH框架的简单上传功能的实现
    笔记:《C++ Primer》第3章
    笔记:《C++ Primer》第1章和第2章
    tcl脚本学习十三:列表命令集
    tcl脚本学习十二: upvar的学习
    tcl脚本学习十一:proc应用 (带默认参数)
    tcl脚本学习十:proc 子函数的使用
    tcl脚本学习八:while循环的使用
    tcl脚本学习九:for循环的学习
    tcl脚本学习七:if的学习
  • 原文地址:https://www.cnblogs.com/xiaoleiel/p/8334771.html
Copyright © 2020-2023  润新知