zoukankan      html  css  js  c++  java
  • google proto buffer安装和简单示例

    google proto buffer安装和简单示例

     

    1、安装

    下载google proto buff

    解压下载的包,并且阅读README.txt,根据里面的指引进行安装。

    $ ./configure

    $ make

    $ make check

    $ make install

    没有意外的话,前面三步应该都能顺利完成,第四步的时候,需要root权限。我采用的默认的路径,所以,仅仅用root权限,还是安装不了,要自己先在/usr/local下新建一个lib的目录,然后执行make install,这样,应该就能顺利安装google proto buffer了。

    安装完后,先写一个测试程序来测试下安装,先来看看proto文件:

    package hello;

    message Hello

    {

    required int32 id = 1; //user id

    required string name = 2; //user name

    optional string email = 3; //user email

    }

    接着,要用protoc生成一个对应的类,我把它生成在./out目录里:

    protoc hello.proto --cpp_out=./out

    接下来,在out目录下,会生成两个文件:

    $> ls

    hello.pb.cc hello.pb.h

    接下来,编写测试用的c++代码:

    hello.cc

    #include <stdio.h>

    #include <string.h>

    #include "out/hello.pb.h"

    using namespace std;

    using namespace hello;

    int main()

    {

    Hello a;

    a.set_id(101);

    a.set_name("xg");

    string tmp;

    bool ret = a.SerializeToString(&tmp);

    if (ret)

    {

    printf("encode success! ");

    }

    else

    {

    printf("encode faild! ");

    }

    Hello b;

    ret = b.ParseFromString(tmp);

    if (ret)

    {

    printf("decode success! id= %d name = %s ", b.id(), b.name().c_str());

    }

    else

    {

    printf("decode faild! ");

    }

    return 0;

    }

    接着,编译一下这个代码,由于使用了protobuf的库,所以编译的时候,要把这些库也链接进来:

    g++ hello.cc ./out/hello.pb.cc -o hello -I./out -I/usr/local/protobuf/include -L/usr/local/lib -lprotobuf

    这样,就生成了测试程序。

    运行一下:

    $> ./hello

    encode success!

    decode success!

    id= 101

    name = xg

    原文

     

    Go中使用protobuf

    https://github.com/golang/protobuf

    iconv -f gb2312 -t utf-8 student_client.proto > s.proto  转换字符集,必须是uft8编码才能使用protoc --go_out=命令

    protoc --go_out=. student_client.proto

  • 相关阅读:
    Oracle中对多行查询结果进行拼接
    DX使用随笔--NavBarControl
    DX使用随记--其他
    DX使用随记--GroupControl
    DX使用随记--ImageComboBoxEdit
    DX使用随记--TabControl
    Oracle相关
    DX使用随记--GridControl
    一个NPOI导出到excel文件的范例记录
    python数据类
  • 原文地址:https://www.cnblogs.com/diegodu/p/4714853.html
Copyright © 2011-2022 走看看