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