美文网首页
linux 安装protobuf

linux 安装protobuf

作者: 鱼小莘 | 来源:发表于2020-03-23 13:41 被阅读0次
    1、根据protobuf GitHub的README.md安装protoBuf

    (1)安装依赖工具

    sudo apt-get install autoconf automake libtool curl make g++ unzip
    

    (2)在protobuf github上获取版本信息及源码链接,用wget下载:

    wget  https://github.com/protocolbuffers/protobuf/releases/download/v3.7.1/protobuf-cpp-3.7.1.tar.gz
    

    (3)解压

    tar -xzvf protobuf-cpp-3.7.1.tar.gz
    
    image.png

    (4)根据官网安装教程依次执行

    cd protobuf-3.7.1
    ./autogen.sh
    ./configure
    make
    make check
    sudo make install
    sudo ldconfig
    

    成功安装


    image.png
    2、定义proto文件,以student.proto为例
    image.png
    3、编译student.proto
    protoc student.proto --cpp_out=./
    
    image.png
    4、protoc会根据定义的proto文件生成相应的API,对于student.id有

    inline bool has_id() const;
    inline void clear_id();
    static const int kIdFieldNumber = 1;
    inline google::protobuf::uint64 id() const;
    inline void set_id(google::protobuf::uint64 value);

    5、除了字段的API,protoc还会生成标准消息函数

    bool IsInitialized() const; //检查是否全部的required字段都被置(set)了值
    void CopyFrom(const Person& from); //用外部消息的值,覆写调用者消息内部的值
    void Clear(); //将所有项复位到空状态
    int ByteSize() const; //消息字节大小
    string DebugString() const; //将消息内容以可读的方式输出
    string ShortDebugString() const; //输出时会有较少的空白
    bool SerializeToString(string* output) const; //将消息序列化并储存在指定的string中
    bool ParseFromString(const string& data); //从给定的string解析消息
    bool SerializeToArray(void * data, int size) const //将消息序列化至数组
    bool ParseFromArray(const void * data, int size) //从数组解析消息
    bool SerializeToOstream(ostream* output) const; //将消息写入到给定的C++ ostream中
    bool ParseFromIstream(istream* input); //从给定的C++ istream解析消息

    相关文章

      网友评论

          本文标题:linux 安装protobuf

          本文链接:https://www.haomeiwen.com/subject/bqhwyhtx.html