Google proto buffer installation and simple example

Source: Internet
Author: User

1 , installation

Download Google Proto Buff .

unzip the downloaded package and read the README.txt , according to the instructions inside the installation.

$./configure

$ make

$ make Check

$ make Install

no accident, the first three steps should be completed smoothly, the fourth step, the need for root permissions. I adopt the default path, so, only with root permissions, or can not install, you must first Create a new Lib under the/usr/local directory, and then perform the make installso that google Proto buffer should be installed successfully .

after the installation, first write a test program to test the installation, first look at Proto File:

Package Hello;

Message Hello

{

Required Int32 id = 1; User ID

Required String name = 2; User name

Optional String email = 3; User Email

}

Next, use Protoc. generate a corresponding class, I generate it in the . /Out in the catalogue:

Protoc Hello.proto--cpp_out=./out

next, in the Out directory, two files are generated :

$> ls

hello.pb.cc Hello.pb.h

Next, write the test C + + Code:

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!\n");

}

Else

{

printf ("Encode faild!\n");

}

Hello b;

RET = b.parsefromstring (TMP);

off (ret)

{

printf ("Decode success!\n id=%d \ n name =%s\n", b.ID (), B.name (). C_STR ());

}

Else

{

printf ("Decode faild!\n");

}

return 0;

}

Next, compile this code, due to the use of Protobuf Library, so when compiling, you should also link these libraries in:

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

In this way, a test program is generated.

Run it:

$>./hello

Encode success!

Decode success!

Id= 101

name = xg

Original

Google proto buffer installation and simple example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.