• protobuf


    1.下载protobuf

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

    或者百度网盘下载protobuf3.7.1 提取码: wfu5

    下载后,解压文件protobuf-cpp-3.7.1.tar.gz,在解压得到的protobuf-3.7.1目录下执行以下命令,--prefix指定安装的根目录

    $ ./configure --prefix=/usr/local

    $ make

    $ make check

    $ sudo make install

    # ldconfig

    2. 测试

    person.proto

    syntax = "proto3";
    
    package person;
    message Person{
        string id=1;
        string name=2;
        string addr = 3;
        string test = 1000;
    }
    
    //protoc --cpp_out=. person.proto
    /*!
    * Auth: xor
    * Date: 2019-6-9
    * File: personWrite.cpp
    * Class: %{Cpp:License:ClassName} (if applicable)
    * Brief:
    * Note:
     */
    #include "person.pb.h"
    #include <iostream>
    #include <fstream>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <string>
    #include <unistd.h>
    
    using namespace std;
    using namespace person;
    
    int main(int argc,char **argv){
        person::Person person;
        person.set_id("111");
        person.set_name("China");
        person.set_addr("Aisa");
        person.set_test("ttttt");
    
        // serialize
        string output = "";
        if(!person.SerializeToString(&output))
        {
            perror("failed to write person.
    ");
            return -1;
        }
    
        cout << "output:" << output << endl;
    
        // serialize data to file
        fstream os("./person.info", ios::out | ios::trunc | ios::binary);
        if(!person.SerializeToOstream(&os))
        {
            perror("failed to write person");
            return -1;
        }
        printf("hello world!
    ");
        return 0;
    }
    
    // g++11 personWrite.cpp person.pb.cc -o xpW -lprotobuf -lpthread
    #include "person.pb.h"
    #include <iostream>
    #include <fstream>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <string>
    #include <unistd.h>
    
    using namespace std;
    using namespace person;
    
    int main(int argc,char **argv){
        person::Person person;
        int fd = open("./person.info", O_EXCL);
        assert(fd > 0);
    
        if(!person.ParseFromFileDescriptor(fd))
        {
            perror("failed to parse msg");
            return -1;
        }
    
        cout << "output:" << person.id() << " name:" << person.name() << " addr:" << person.addr() << " test:" << person.test() << endl;
    
        printf("hello world!
    ");
        return 0;
    }
    
    // g++11 personRead.cpp person.pb.cc -o xpR -lprotobuf -lpthread
  • 相关阅读:
    Spark Interaction(特征交互-笛卡尔转换)
    Spark DCT 离散余弦变换
    Spark polynomialExpansion 多项式扩展
    Spark PCA
    Spark n-gram模型
    Spark OneHotEncoder
    Spark 逻辑回归LogisticRegression
    查看macOS下正在使用的zsh
    Neovim中NERDTree等多处cursorline不高亮
    让pip使用python3而不是python2
  • 原文地址:https://www.cnblogs.com/guxuanqing/p/10883268.html
Copyright © 2020-2023  润新知