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. 196D The Next Good String

    传送门 题目大意 给定n和一个字符串,求一个新字符串使得这个字符串不存在长度大于等于n的回文子串且在字典序大于原串的情况下最小. 分析 我们知道如果有一个长度为n+2的回文串,那它一定由一个长度为n的 ...

  2. String、StringBuffer与StringBuilder之间区别 .RP

    最近学习到StringBuffer,心中有好些疑问,搜索了一些关于String,StringBuffer,StringBuilder的东西,现在整理一下. 关于这三个类在字符串处理中的位置不言而喻,那 ...

  3. Jsp入门第二天

    1. JSP 指令: JSP指令(directive)是为JSP引擎而设计的. 它们并不直接产生任何可见输出, 而只是告诉引擎如何处理JSP页面中的其余部分. 2. 在目前的JSP 2.0中,定义了p ...

  4. 【Android开源】CircleImageView自定义圆形控件的使用

    github地址:https://github.com/hdodenhof/CircleImageView package de.hdodenhof.circleimageview; import e ...

  5. css笔记-1

    id 优先级大于 class 行间 style  优先级大于id class 和属性是并行的 !important > 行间样式 >id >class|属性 >标签选择器 &g ...

  6. unity编辑器拓展

    [ExecutelnEditMode]     在EditMode下也可以执行脚本,Unity默认情况下,脚本只有运行时被执行,加上此属性后,不运行程序也能执行.与PlayMode不同的是函数不会不停 ...

  7. wffmpeg64.dll调用 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。

    求解中.....

  8. Kotlin 数据类型(数值类型)

    Kotlin 的常见数据类型: 类型 范围 byte -128~127 short 32767-32768 int -2147483648~2147483647 long 92233720368547 ...

  9. 正经学C#_位移与其位移运算符[c#入门经典]

    在c#入门经典一书中,最为糟糕的一节就是位移了,完全没有讲明白,也没有说全,似乎只是轻轻点了一下何为位移,带了两次原码和补码,完全不理会是否明白不明白.这一点这本书很差.因为此书说了,在大多数应用开发 ...

  10. MySQL的ODBC安装错误问题!

    MySQL的ODBC安装时候可能会出错,主要原因是缺少VC支持库,需要2010版本的VC支持库!!X86和X64分别对应MySQL对应的ODBC,不能安装一个两个都搞定,如果需要安装两个ODBC驱动, ...