https://blog.csdn.net/wangyin668/article/details/80046798

https://www.cnblogs.com/zhouyang209117/p/7218719.html

https://blog.csdn.net/zmzsoftware/article/details/17356199

自己云服务器上有demo learing/Protobuf //SRC_DIR .proto文件存放目录
//--cpp_out 指示编译器生成C++代码,DST_DIR为生成文件存放目录
//XXXX.proto 待编译的协议文件
protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/XXXX.proto

  

syntax = "proto2";
package tutorial;
message Person {
required string name = ;
required int32 id = ;
optional string email = ;
enum PhoneType {
MOBILE = ;
HOME = ;
WORK = ;
}
message PhoneNumber {
required string number = ;
optional PhoneType type = [default = HOME];
}
repeated PhoneNumber phones = ;
}
message AddressBook {
repeated Person people = ;
}

 把proto文件转化成c++代码.执行下面命令.

protoc --cpp_out=. addressbook.proto   
#include <iostream>
#include <fstream>
#include <string>
#include "addressbook.pb.h"
using namespace std;
// 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(, '\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;
}
}
}
// 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 != ) {
cerr << "Usage: " << argv[] << " ADDRESS_BOOK_FILE" << endl;
return -;
}
tutorial::AddressBook address_book;
{
// Read the existing address book.
fstream input(argv[], ios::in | ios::binary);
if (!input) {
cout << argv[] << ": File not found. Creating a new file." << endl;
} else if (!address_book.ParseFromIstream(&input)) {
cerr << "Failed to parse address book." << endl;
return -;
}
}
// Add an address.
PromptForAddress(address_book.add_people());
{
// Write the new address book back to disk.
fstream output(argv[], ios::out | ios::trunc | ios::binary);
if (!address_book.SerializeToOstream(&output)) {
cerr << "Failed to write address book." << endl;
return -;
}
}
// Optional: Delete all global objects allocated by libprotobuf.
google::protobuf::ShutdownProtobufLibrary();
return ;
}
#include <iostream>
#include <fstream>
#include <string>
#include "addressbook.pb.h"
using namespace std;
// Iterates though all people in the AddressBook and prints info about them.
void ListPeople(const tutorial::AddressBook &address_book) {
for (int i = ; 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.has_email()) {
cout << " E-mail address: " << person.email() << endl;
}
for (int j = ; 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;
}
cout << phone_number.number() << 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 != ) {
cerr << "Usage: " << argv[] << " ADDRESS_BOOK_FILE" << endl;
return -;
}
tutorial::AddressBook address_book;
{
// Read the existing address book.
fstream input(argv[], ios::in | ios::binary);
if (!address_book.ParseFromIstream(&input)) {
cerr << "Failed to parse address book." << endl;
return -;
}
}
ListPeople(address_book);
// Optional: Delete all global objects allocated by libprotobuf.
google::protobuf::ShutdownProtobufLibrary();
return ;
}
g++ addressbook.pb.cc   read_data.cpp   -o  read.out     -lprotobuf
g++ addressbook.pb.cc write_data.cpp -o write.out -lprotobuf

protobuf在c++的使用方法以及在linux安装的更多相关文章

  1. protobuf与json相互转换的方法

    google的protobuf对象转json,不能直接使用FastJson之类的工具进行转换,原因是protobuf生成对象的get方法,返回的类型有byte[],而只有String类型可以作为jso ...

  2. 详解Linux安装GCC方法

    转载自:http://blog.csdn.net/bulljordan23/article/details/7723495/ 下载: http://ftp.gnu.org/gnu/gcc/gcc-4. ...

  3. Linux安装配置php环境的方法

    本文实例讲述了Linux安装配置php环境的方法.分享给大家供大家参考,具体如下: 1.获取安装文件: http://www.php.net/downloads.php php-5.3.8.tar.g ...

  4. linux安装IPython四种方法

    IPython是Python的交互式Shell,提供了代码自动补完,自动缩进,高亮显示,执行Shell命令等非常有用的特性.特别是它的代码补完功能,例如:在输入zlib.之后按下Tab键,IPytho ...

  5. 为你详解Linux安装GCC方法

    下载: http://ftp.gnu.org/gnu/gcc/gcc-4.5.1/gcc-4.5.1.tar.bz2浏览: http://ftp.gnu.org/gnu/gcc/gcc-4.5.1/查 ...

  6. iphone app的非appstore发布方法及其免越狱安装方法

    iphone app的非appstore发布方法及其免越狱安装方法   本文包含两项内容, 1.开发者如何将app导出为可供普通用户在外部安装的ipa文件. 2.用户使用itools来安装ipa格式的 ...

  7. linux安装教程以及使用时遇到的问题和解决方法

    以后开发都是要用linux,所以就安装了ubuntu,也是第一次用linux的系统.装的是win7+Ubuntu16.04的双系统. 安装过程如下:我用的是U盘安装,参看http://www.jian ...

  8. 虚拟机中的Linux安装VMware Tools的方法

    先检查虚拟机是否能上网 一:安装VMware Tools的之前必装的工具套件方法如下: Centos安装VMware Tools: [root@piaoyun-vm vmware-tools-dist ...

  9. linux安装mysql服务分两种安装方法:

    linux安装mysql服务分两种安装方法: ①源码安装,优点是安装包比较小,只有十多M,缺点是安装依赖的库多,安装编译时间长,安装步骤复杂容易出错: ②使用官方编译好的二进制文件安装,优点是安装速度 ...

随机推荐

  1. Django rest-framework框架十大功能分析

    rest-framework框架有哪些作用? 一共有十点. 路由 - 可以通过as_view传参数,根据请求方式不同执行相应的方法 - 可以在url中设置一个结尾,类似于: .json 视图 - 帮助 ...

  2. 优先队列详解priority_queue .RP

    ) 删除.在最小优先队列(min priorityq u e u e)中,查找操作用来搜索优先权最小的元素,删除操作用来删除该元素;对于最大优先队列(max priority queue),查找操作用 ...

  3. (字符串)count and say

    https://www.nowcoder.com/practice/c5e8e84b62bb48398ec3c88153950fb5?tpId=46&tqId=29141&tPage= ...

  4. Mysql--连接查询

    内连接查询 意义:找到表和表之间的关系或者是桥梁.连接查询是查询两个或者两个以上的表时使用的. JOIN|CROSS JOIN| INNER JOIN    通过ON  连接条件(这三个方式都行)一般 ...

  5. MySQL中MyISAM引擎与InnoDB引擎性能简单测试

    [硬件配置]CPU : AMD2500+ (1.8G)内存: 1G/现代硬盘: 80G/IDE[软件配置]OS : Windows XP SP2SE : PHP5.2.1DB : MySQL5.0.3 ...

  6. java 抽象方法

    int 是基本数据类型Integer是其包装类,注意是一个类.为什么要提供包装类呢???一是为了在各种类型间转化,通过各种方法的调用.否则 你无法直接通过变量转化.比如,现在int要转为Stringi ...

  7. 小小c#算法题 - 7 - 堆排序 (Heap Sort)

    在讨论堆排序之前,我们先来讨论一下另外一种排序算法——插入排序.插入排序的逻辑相当简单,先遍历一遍数组找到最小值,然后将这个最小值跟第一个元素交换.然后遍历第一个元素之后的n-1个元素,得到这n-1个 ...

  8. JetBrains WebStorm 如何从GitHub上克隆的代码

    工作中经常会遇到要从GitHub上拉代码,详细操作记录如下: 绑定账号 1.File->Settings->Version Control->Github 成功后会出现下面的这个账户 ...

  9. c# 大文件分割 复制 Filestream 进度条

    大文件分割复制,每次复制100M 也可以复制别的较大数值. 小于1G的小文件就直接复制得了.代码里没写 ,但是很简单 直接写进去就好了,难得是分割复制 所以没写. 好吧 我还是改了 改成小文件也可以复 ...

  10. 转载黑客是如何黑到你手机的?绝对涨姿势,一位黑客的Wi-Fi入侵实录!

        声明:这是一虚构的故事,因此对图片均进行了模糊化处理.内容整理自网络! 故事的主人公小黑是一名从事IT相关工作的技术宅男.五一长假来临,宅在家中的他相当无聊,打开手机上的Wi-Fi模块,发现附 ...