zoukankan      html  css  js  c++  java
  • Sword protobuf学习一

    protobuf简介
        Protocol Buffers,是Google公司开发的一种数据描述语言,类似于XML能够将结构化数据序列化,可用于数据存储、通信协议等方面。
    它不依赖于语言和平台并且可扩展性极强。现阶段官方支持C++、JAVA、Python三种编程语言,但可以找到大量的几乎涵盖所有语言的第三方拓展包。
    #protobuf编译流程
    
    1.确定centos7上已经安装了下面的软件,或者直接用yum进行更新
    autoconf
    automake
    libtool
    curl (used to download gmock)
    make
    g++
    unzip
    
    2.下载源码包,解压,编译安装
    地址:https://github.com/google/protobuf/releases
    选择Source code (tar.gz)下载
    >>tar -zxvf protobuf-3.1.0.tar.gz -C /usr/local/
    >>cd protobuf-3.1.0/
    >>./autogen.sh
    # 指定安装路径
    >>./configure --prefix=/usr/local/protobuf
    #编译
    >>make
    # 测试,这一步很耗时间--不执行测试也无所谓
    >>make check 
    >>make install
    
    3.编写proto文件
    
    4.执行build.sh脚本,生成相关c文件
    
    5.业务逻辑编写
    google protobuff动静态库问题
    
    在Linux上编译google protobuff时,configure 默认选项是生成动态库,即libprotobuf.so文件。
    如果程序以dlopen方式多次加载libprotobuf.so,并且使用的是同一个protobuff结构,则运行时将会报错
    [libprotobuf ERROR google/protobuf/descriptor_database.cc:58] File already exists in database: google/protobuf/descriptor.proto 
    [libprotobuf FATAL google/protobuf/descriptor.cc:1394] 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): Aborted (core dumped)
    为了解决这个问题,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文件,找到以下代码
    if test "x${ac_cv_env_CFLAGS_set}" = "x"; then :
      CFLAGS=""
    fi
    if test "x${ac_cv_env_CXXFLAGS_set}" = "x"; then :
      CXXFLAGS=""
    fi
    #添加编译选项:-fPIC
    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"
    fi
    重新编译google protobuff
    
    总结:google protobuff最好使用静态库
  • 相关阅读:
    计算机网络
    计算机网络知识总结
    final,static,super,this
    ArrayList
    基础面试题
    BATJ都爱问的多线程面试题
    Session过期、失效时间
    类加载器ClassLoader源码解析
    连接ftp服务器 JDK 1.7
    根据当前请求的特征,判断该请求是否来自手机终端
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/9689520.html
Copyright © 2011-2022 走看看