一、静态使用proto文件步骤
1、创建proto文件 addressbook.proto
2、使用protoc编译器将proto文件编译成C++代码
编译命令格式:protoc -I=$SRC_DIR --cpp_out=$DST_DIR /path/to/file.proto
列如:protoc -I=. --cpp_out=. addressbook.proto
3、编写操作proto文件对应的C++程序
4、编译C++程序
g++ -std=c++11 addressbook.pb.cc main.cc -o main -lprotobuf(编译没有问题,运行报错,原因未知)
g++ -std=c++11 addressbook.pb.cc main.cc -o main -lprotobuf -lpthread(编译和运行都正常)
例如:example01
// Filename: addressbook.proto syntax="proto2"; package addressbook; message Person { 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; }
//main.cc #include <iostream> #include "addressbook.pb.h" int main(int argc, const char* argv[]) { addressbook::AddressBook person; addressbook::Person* pi = person.add_person_info(); pi->set_name("aut"); pi->set_id(1219); std::cout << "before clear(), id = " << pi->id() << std::endl; pi->clear_id(); std::cout << "after clear(), id = " << pi->id() << std::endl; pi->set_id(1087); if (!pi->has_email()) pi->set_email("autyinjing@126.com"); addressbook::Person::PhoneNumber* pn = pi->add_phone(); pn->set_number("021-8888-8888"); pn = pi->add_phone(); pn->set_number("138-8888-8888"); pn->set_type(addressbook::Person::MOBILE); uint32_t size = person.ByteSize(); unsigned char byteArray[size]; person.SerializeToArray(byteArray, size); addressbook::AddressBook help_person; help_person.ParseFromArray(byteArray, size); addressbook::Person help_pi = help_person.person_info(0); std::cout << "*****************************" << std::endl; std::cout << "id: " << help_pi.id() << std::endl; std::cout << "name: " << help_pi.name() << std::endl; std::cout << "email: " << help_pi.email() << std::endl; for (int i = 0; i < help_pi.phone_size(); ++i) { auto help_pn = help_pi.mutable_phone(i); std::cout << "phone_type: " << help_pn->type() << std::endl; std::cout << "phone_number: " << help_pn->number() << std::endl; } std::cout << "*****************************" << std::endl; return 0; }