前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >protobuf的安装及使用

protobuf的安装及使用

作者头像
yifei_
发布2022-11-14 14:31:23
1.1K0
发布2022-11-14 14:31:23
举报
文章被收录于专栏:yifei的专栏

Linux下protobuf的安装过程&简单使用。 protobuf是google开发的一个灵活的、高效的用于序列化数据的协议。相比较XML和JSON格式,protobuf更小、更快、更便捷。

安装

下载链接:https://github.com/protocolbuffers/protobuf/releases ,我是用c++,所以下载protobuf-cpp就行了。

tar -xzvf protobuf-*.tar.gz

cd protobuf-*

./configure –prefix=/usr/local/protobuf

make (可能会等好一段时间)

make check

make install

vim /etc/profile

代码语言:javascript
复制
export PATH=$PATH:/usr/local/protobuf/bin/
export PKG_CONFIG_PATH=/usr/local/protobuf/lib/pkgconfig/

source /etc/profile

protoc –version #测试是否安装成功

使用方法

代码语言:javascript
复制
cd ~/test

vim address.proto

代码语言:javascript
复制
package tutorial;

message Persion {
    required string name = 1;
    required int32 age = 2;
}

message AddressBook {
    repeated Persion persion = 1;
}

protoc -I=. –cpp_out=. address.proto #在当前文件夹生成cpp的.h和.cc文件

vim write.cpp

代码语言:javascript
复制
#include <iostream>
#include <fstream>
#include <string>
#include "address.pb.h"

using namespace std;

void PromptForAddress(tutorial::Persion *persion) {
    cout << "Enter persion name:" << endl;
    string name;
    cin >> name;
    persion->set_name(name);

    int age;
    cin >> age;
    persion->set_age(age);
}

int main(int argc, char **argv) {
    //GOOGLE_PROTOBUF_VERIFY_VERSION;

    if (argc != 2) {
        cerr << "Usage: " << argv[0] << " ADDRESS_BOOL_FILE" << endl;
        return -1;
    }

    tutorial::AddressBook address_book;

    {
        fstream input(argv[1], ios::in | ios::binary);
        if (!input) {
            cout << argv[1] << ": File not found. Creating a new file." << endl;
        }
        else if (!address_book.ParseFromIstream(&input)) {
            cerr << "Filed to parse address book." << endl;
            return -1;
        }
    }

    // Add an address
    PromptForAddress(address_book.add_persion());

    {
        fstream output(argv[1], ios::out | ios::trunc | ios::binary);
        if (!address_book.SerializeToOstream(&output)) {
            cerr << "Failed to write address book." << endl;
            return -1;
        }
    }

    // Optional: Delete all global objects allocated by libprotobuf.
    //google::protobuf::ShutdownProtobufLibrary();

    return 0;
}

read.cpp

代码语言:javascript
复制
#include <iostream>
#include <fstream>
#include <string>
#include "address.pb.h"

using namespace std;

void ListPeople(const tutorial::AddressBook& address_book) {
    for (int i = 0; i < address_book.persion_size(); i++) {
        const tutorial::Persion& persion = address_book.persion(i);

        cout << persion.name() << " " << persion.age() << endl;
    }
}

int main(int argc, char **argv) {
    //GOOGLE_PROTOBUF_VERIFY_VERSION;

    if (argc != 2) {
        cerr << "Usage: " << argv[0] << " ADDRESS_BOOL_FILE" << endl;
        return -1;
    }

    tutorial::AddressBook address_book;

    {
        fstream input(argv[1], ios::in | ios::binary);
        if (!address_book.ParseFromIstream(&input)) {
            cerr << "Filed to parse address book." << endl;
            return -1;
        }
        input.close();
    }

    ListPeople(address_book);

    // Optional: Delete all global objects allocated by libprotobuf.
    //google::protobuf::ShutdownProtobufLibrary();

    return 0;
}

编译两个文件

代码语言:javascript
复制
g++ address.pb.cc write.cpp -o write `pkg-config --cflags --libs protobuf`
g++ address.pb.cc read.cpp -o read `pkg-config --cflags --libs protobuf`

运行

代码语言:javascript
复制
./write data.txt
aaa 999
./read data.txt
aaa 999

问题

运行write时,提示找不到动态链接库

代码语言:javascript
复制
添加安装路径下的lib路径
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/protobuf/lib

编码示例

代码语言:javascript
复制
package test
message hello {
   int32 id = 1;//id=300
   string str = 2;//str="hello"
}

key 1 >> 1000|0 >> 1000	>> 08
yield 300 >> ‭0010   010 1100‬ >> 010 1100   000 0010 >> 1010 1100   0000 0010 >> ac 02

key 2 >> 10000|10 >> 0001 0010 >> 12
yield hello 长度05
	>> 68 65 6c 6c 6f
	>> 01101000  01100101 01101100 01101100 01101111
	>> 
result: 08 ac 02 12 05 68 65 6c 6c 6f

参考

欢迎与我分享你的看法。 转载请注明出处:http://taowusheng.cn/

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-07-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装
  • 使用方法
  • 问题
  • 编码示例
  • 参考
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档