ProtoBuf (full name Protocol Buffer) is a data structure serialization and deserialization framework, which has the following features:
.proto
file to define the structure object (message) and attribute contentprotoc
compiler to compile the .proto file and generate a series of interface codes, which are stored in the newly generated header file and source file.proto
file, and serialization and deserialization of the message
object.contacts.proto
is defined as follows.
syntax = "proto3"; // Declaration syntax version
package contacts; // Declaring a namespace
// Description of structured object
message contact {
// Description of each field
/* Field type Field name = Field unique number */
uint64 sn = 1;
string name = 2;
float score = 3;
};
After defining the proto file, we need to let proto generate the corresponding .h
and .cc
for us.
protoc --cpp_out=. contacts.proto
This will generate successfully.
namespace contacts {
PROTOBUF_CONSTEXPR contact::contact(
The namespace here is the name of the package we determined.
So how to call and use it?
Note that you need to link it when using it.
makefile
test: main.cc contacts.pb.cc
g++ -o $@ $^ -lprotobuf -std=c++11
.PHONY:clean
clean:
rm -f test
main.cc
int main() {
contacts::contact conn;
conn.set_sn(10001);
conn.set_name("Sam");
conn.set_score(60.5);
std::string str = conn.SerializeAsString();
// conn.SerializeToString(&str); // same
contacts::contact stu;
bool ret = stu.ParseFromString(str);
if (ret == false) // Deserialization failed
assert(false);
std::cout << stu.sn() << " " << stu.name() << " " << stu.score() << std::endl;
return 0;
}
This is a basic usage.