最全google protobuf (c++) 学习_c++使用google protobuf,C C++布局优化之include、merge、ViewStub的使用

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

import “google/protobuf/timestamp.proto”;
// [END declaration]

// [START java_declaration]
option java_package = “com.example.tutorial”;
option java_outer_classname = “AddressBookProtos”;
// [END java_declaration]

// [START csharp_declaration]
option csharp_namespace = “Google.Protobuf.Examples.AddressBook”;
// [END csharp_declaration]

// [START messages]
message Person {
string name = 1;
int32 id = 2; // Unique ID number for this person.
string email = 3;

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

message PhoneNumber {
string number = 1;
PhoneType type = 2;
}

repeated PhoneNumber phones = 4;

google.protobuf.Timestamp last_updated = 5;
}

// Our address book file is just one of these.
message AddressBook {
repeated Person people = 1;
}
// [END messages]


之后对其进行编译,产生对应的xxx.pb.cc和xxx.pb.h文件。



protoc -I=./ --cpp_out=./ addressbook.proto


最后,


1. 编写add\_person.cc文件,该应用程序实现控制台输入person的相关信息并写入到文本文件:



// See README.txt for information and build instructions.

#include
#include
#include <google/protobuf/util/time_util.h>
#include
#include

#include “addressbook.pb.h”

using namespace std;

using google::protobuf::util::TimeUtil;

// This function fills in a Person message based on user input.
void PromptForAddress(tutorial::Person* person) {
cout << "Enter person ID number: ";
int id;
cin >> id;
person->set_id(id);
cin.ignore(256, ‘\n’);

cout << "Enter name: ";
getline(cin, *person->mutable_name());

cout << "Enter email address (blank for none): ";
string email;
getline(cin, email);
if (!email.empty()) {
person->set_email(email);
}

while (true) {
cout << "Enter a phone number (or leave blank to finish): ";
string number;
getline(cin, number);
if (number.empty()) {
break;
}

tutorial::Person::PhoneNumber\* phone_number = person->add\_phones();
phone_number->set\_number(number);

cout << "Is this a mobile, home, or work phone? ";
string type;
getline(cin, type);
if (type == "mobile") {
  phone_number->set\_type(tutorial::Person::MOBILE);
} else if (type == "home") {
  phone_number->set\_type(tutorial::Person::HOME);
} else if (type == "work") {
  phone_number->set\_type(tutorial::Person::WORK);
} else {
  cout << "Unknown phone type. Using default." << endl;
}

}
*person->mutable_last_updated() = TimeUtil::SecondsToTimestamp(time(NULL));
}

// Main function: Reads the entire address book from a file,
// adds one person based on user input, then writes it back out to the same
// file.
int main(int argc, char* argv[]) {
// Verify that the version of the library that we linked against is
// compatible with the version of the headers we compiled against.
GOOGLE_PROTOBUF_VERIFY_VERSION;

if (argc != 2) {
cerr << “Usage: " << argv[0] << " ADDRESS_BOOK_FILE” << endl;
return -1;
}

tutorial::AddressBook address_book;

{
// Read the existing address book.
fstream input(argv[1], ios::in | ios::binary);
if (!input) {
cout << argv[1] << “: File not found. Creating a new file.” << endl;
} else if (!address_book.ParseFromIstream(&input)) {
cerr << “Failed to parse address book.” << endl;
return -1;
}
}

// Add an address.
PromptForAddress(address_book.add_people());

{
// Write the new address book back to disk.
fstream output(argv[1], ios::out | ios::trunc | ios::binary);
if (!address_book.SerializeToOstream(&output)) {
cerr << “Failed to write address book.” << endl;
return -1;
}
}

// Optional: Delete all global objects allocated by libprotobuf.
google::protobuf::ShutdownProtobufLibrary();

return 0;
}


2. 编写list\_people.cc文件,该应用程序实现将add\_person.cc写入的文本文件进行可视化读取。



// See README.txt for information and build instructions.

#include
#include <google/protobuf/util/time_util.h>
#include
#include

#include “addressbook.pb.h”

using namespace std;

using google::protobuf::util::TimeUtil;

// Iterates though all people in the AddressBook and prints info about them.
void ListPeople(const tutorial::AddressBook& address_book) {
for (int i = 0; i < address_book.people_size(); i++) {
const tutorial::Person& person = address_book.people(i);

cout << "Person ID: " << person.id() << endl;
cout << " Name: " << person.name() << endl;
if (person.email() != "") {
  cout << " E-mail address: " << person.email() << endl;
}

for (int j = 0; j < person.phones\_size(); j++) {
  const tutorial::Person::PhoneNumber& phone_number = person.phones(j);

  switch (phone_number.type()) {
    case tutorial::Person::MOBILE:
      cout << " Mobile phone #: ";
      break;
    case tutorial::Person::HOME:
      cout << " Home phone #: ";
      break;
    case tutorial::Person::WORK:
      cout << " Work phone #: ";
      break;
    default:
      cout << " Unknown phone #: ";
      break;
  }
  cout << phone_number.number() << endl;
}
if (person.has\_last\_updated()) {
  cout << " Updated: " << TimeUtil::ToString(person.last\_updated()) << endl;
}

}
}

// Main function: Reads the entire address book from a file and prints all
// the information inside.
int main(int argc, char* argv[]) {
// Verify that the version of the library that we linked against is
// compatible with the version of the headers we compiled against.
GOOGLE_PROTOBUF_VERIFY_VERSION;

if (argc != 2) {
cerr << “Usage: " << argv[0] << " ADDRESS_BOOK_FILE” << endl;
return -1;
}

tutorial::AddressBook address_book;

{
// Read the existing address book.
fstream input(argv[1], ios::in | ios::binary);
if (!address_book.ParseFromIstream(&input)) {
cerr << “Failed to parse address book.” << endl;
return -1;
}
}

ListPeople(address_book);

// Optional: Delete all global objects allocated by libprotobuf.
google::protobuf::ShutdownProtobufLibrary();

return 0;
}


分别进行编译,产生对应的可执行文件



c++ add_person.cc addressbook.pb.cc -o add_person_cpp pkg-config --cflags --libs protobuf
c++ list_people.cc addressbook.pb.cc -o list_people_cpp pkg-config --cflags --libs protobuf


分别运行



./add_person_cpp address_book_file
./list_people_cpp address_book_file


![在这里插入图片描述](https://img-blog.csdnimg.cn/20190613194148594.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM0NTcxNjc=,size_16,color_FFFFFF,t_70)  
 之后对写入的文件进行读取  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20190613194220685.png)


##### 三、protobuf文件生成分析


参考:



![img](https://img-blog.csdnimg.cn/img_convert/d6edd9b757a0aebbf2e3aea83b00d249.png)
![img](https://img-blog.csdnimg.cn/img_convert/b5db320778c30e0035829fe578d4fd6f.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618668825)**




[外链图片转存中...(img-1gqwKY2Y-1715815376052)]
[外链图片转存中...(img-4CQSTwZC-1715815376052)]

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618668825)**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值