zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    几个关于设计的小问题
    基于建立/保持时间等的参数化时序分析
    Stratix内嵌存储器测试报告
    采用流水线技术实现8位加法器
    运算顺序引发的一系列有趣问题
    PON系统基础知识简介
    某MDU产品OMCI软件升级加速方案
    研究生期间接受的指导(二)
    研究生期间接受的指导(一)
    1063 Set Similarity (25 分)
  • 原文地址:https://www.cnblogs.com/guxuanqing/p/10883268.html
Copyright © 2011-2022 走看看