Protobuf novice tutorial

Keywords: protobuf

Protobuf is a lightweight and efficient structured data storage format officially produced by Google, which can be used for structured data serialization, or serialization. It is very suitable for data storage or RPC data exchange format. It can be used for platform independent, language independent and extensible serialization structure data format in communication protocol, data storage and other fields.


  • Platform independent, language independent, extensible

  • It provides a friendly dynamic library and is easy to use

  • The parsing speed is fast, about 20-100 times faster than the corresponding XML

  • Serialized data is very concise and compact. Compared with XML, the amount of serialized data is about 1 / 3 to 1 / 10


Protobuf has high efficiency for messages below 1M, but when messages are large pieces of data greater than 1M, protobuf does not perform well. Please use it reasonably.

Proto file

To use protobuf, you need to write a. Proto file first, and then compile it. Compiling proto files requires the use of official protoc tools.

// Filename: addressbook.proto

syntax="proto2";  // Indicate use protobuf The compiler version of is v2,The latest version is v3   
package addressbook;

message Person {    //message yes Protobuf Structured data in, similar to C++Class in which you can define the data to be processed
    required string name = 1;
    required int32 id = 2;
    optional string email = 3;

    enum PhoneType {
        MOBILE = 0;
        HOME = 1;
        WORK = 2;

    message PhoneNumber {
        required string number = 1;
        optional PhoneType type = 2 [default = HOME];

    repeated PhoneNumber phone = 4;

message AddressBook {
    repeated Person person_info = 1;


1,2,3 Is the identification number of the field. In the message definition, each field has a unique digital identification number. These identification numbers are used to identify each field in the binary format of the message. Once used, they cannot be changed.
The identification number ranges from 1 to 229 - 1, of which [19000-19999] is reserved for Protobuf and cannot be used.

Person An enum and a message are internally declared, which is similar to the intra class declaration in C + +. The external structure of Person can use PhoneType in the way of Person.PhoneType. When using the structure in the external package, use the format of pkgName.msgName.typeName, and use '.' to connect between every two layers, similar to "::" in C + +.


Scalar type list

proto type C + + type remarks
double double  
float float  
int32 int32 Using variable length encoding is not efficient enough when encoding negative numbers - if the field may contain negative numbers, use sint32
int64 int64 Using variable length encoding is not efficient enough to encode negative numbers - if the field may contain negative numbers, use sint64
uint32 uint32 Use variable length encoding
uint64 uint64 Use variable length encoding
sint32 int32 Using variable length encoding, signed integer values, encoding is more efficient than the usual int32
sint64 int64 Using variable length encoding, signed integer values, encoding is more efficient than the usual int64
fixed32 uint32 Always 4 bytes. If the value is always greater than 228, this type will be more efficient than uint32
fixed64 uint64 Always 8 bytes. If the value is always greater than 256, this type will be more efficient than uint64
sfixed32 int32 Always 4 bytes
sfixed64 int64 Always 8 bytes
bool bool  
string string A string must be UTF-8 encoded or 7-bit ASCII encoded text
bytes string May contain byte data in any order


  How to use C + + development under win10 system

1. Source installation

Visit the latest address and download protobuf-cpp-3.19.1.tar.gz for c + + development; Or


2. Unzip the source code to any path


3. Download CMake tool

4. CMake generate VS2019 project

  • Open CMake

  • Set the cmake directory path/protobuf-3.19.1/cmake under the source code path

  • Set any build directory path/protobuf_build

  • Click configure, select the corresponding VS, compile to WINI32, and the compiler defaults

  • Click the Finish button to start automatic compilation

  • Click Generate to Generate the VS project

  • Open the generated project with VS, and select and compile libprotobuf, libprotobuf Lite, libprotoc and protoc projects as needed

  • In the release or debug directory, find the lib file and protocol.exe

5. protoc.exe uses the command line. proto file to compile and generate the corresponding. h and. cc files

# Standard command
protoc -I=./ --cpp_out=./ ./test.proto
#--cpp_out Is the output path,  ./Is the current path

6. Introduce your own VS2019 project

  • Copy *. and *. pb.h files to the current project

  • Copy the goose folder under src directory to the include directory of the current project

  • Copy libprotobuf.lib libprotoc.lib to the Lib directory of the current project

  • Set the project to multi byte and add a static link library

  • C/C++ General Additional Include Directories

  • Linker General Additional Library Directories add lib directories

  • Linker Input Additional Dependencies add lib field name

7. VS compilation or g + + compilation generates executable code

g++ -I $INCLUDE_PATH -L $LIB_PATH -lprotobuf -pthread


Common API

Protocol defines the following functions for each required field and optional field of message, but not limited to these functions

TypeName xxx() const;           //Get field
// bool has_xxx();              //Determine whether to set the value   pb 2
void set_xxx(const TypeName&);  //set up
void clear_xxx();               //Make it the default

The following are defined for each repeated field:

TypeName* add_xxx();            //Add node
TypeName xxx(int) const;        //Gets the node of the specified sequence number, similar to c++Yes“[]"operator
TypeName* mutable_xxx(int);     //Similar to the previous one, but the pointer is obtained
int xxx_size();                 //Get the number of nodes

Several commonly used serialization functions

bool SerializeToOstream(std::ostream * output) const;   //Output to output stream
bool SerializeToString(string * output) const;          //Output to string
bool SerializeToArray(void * data, int size) const;     //Output to byte stream

Corresponding deserialization function

bool ParseFromIstream(std::istream * input);            //Parse from input stream
bool ParseFromString(const string & data);              //from string analysis
bool ParseFromArray(const void * data, int size);        //Parsing from byte stream

Other common functions

bool IsInitialized();       // Check all required Fields are set to values
size_t ByteSize() const;    // Gets the size of the binary byte order    pb2
official API file:

reference material


Posted by hyperpcs on Fri, 03 Dec 2021 22:08:32 -0800