protobuf/protobuf-c tutorials

1) autotools (autoconf, automake, libtool) must also be installed
sudo apt-get install autoconf automake libtool curl make g++ unzip

2) git clone https://github.com/google/protobuf.git
./autogen.sh
./configure
make && sudo make install
export LD_LIBRARY_PATH=/usr/local/lib

3) git clone https://github.com/protobuf-c/protobuf-c.git
./autogen.sh
./configure
make && sudo make install

4) protoc-c example.proto --c_out=./

5) example
gcc main.c -o main example.pb-c.c -lprotobuf-c -L ./protobuf-c/lib/ -I ./protobuf-c/include/protobuf-c
gcc main.c -o main example.pb-c.c `pkg-config --cflags --libs 'libprotobuf-c >= 1.0.0'`
g++ main.cc -o main example.pb.cc `pkg-config --cflags --libs protobuf`

6) reference
https://github.com/protobuf-c/protobuf-c/blob/master/README.md
https://github.com/protocolbuffers/protobuf/blob/master/src/README.md

7) wiki example
https://github.com/protobuf-c/protobuf-c/wiki/Examples

 

8) pack/unpack

static int pb_encode (Req req, unsigned char **buf, unsigned int *len) 
{ 
  *len = req__get_packed_size (&req);
  *buf = malloc (sizeof (unsigned char) * (*len));
  req__pack (&req, *buf);

  return 0;
}

static int pb_decode (unsigned char *buf, unsigned int len, Req **rreq) 
{ 
  *rreq = req__unpack (NULL, len, buf);
  if (rreq == NULL)
  { 
    fprintf(stderr, "error unpacking incoming message\n");
    return 1;
  }
     
  req__free_unpacked(*rreq,NULL);
  return 0;
}

example:

    Req req = REQ__INIT;
    unsigned char *buf; 
    unsigned int len;
    pb_encode (req, &buf, &len);

    Req *rreq;
    pb_decode (buf, len, &rreq);

 

How to Install Protobuf (protoc or protocol buffers) in Ubuntu?

https://www.hitxp.com/qa/install-protobuf-protoc-ubuntu-python/

 

A simple and complete example of protobuf-c

https://github.com/dong2/2rpc

 

posted @ 2020-11-26 01:20  dong1  阅读(109)  评论(0编辑  收藏  举报