1

使用protobuf 2.x 下载地址(3.x 在c++11 vs2017下报错)

源码 https://github.com/google/protobuf

或者直接下载 二进制文件

2 如果下载的是代码 编译需要使用cmake 来生成VC的工程

cmake的使用从略

编译设置如图

3 如果下载的是代码  开启VC工程编译protobuf  由于以后自写代码需要使用protobuf的LIB 所以编译时请确认编译的版本

我使用的是VC2017编译的 x64 debug 版本

如图 

4 以protobuf中的例子 来进行实验

addressbook.proto

内容如下

 // See README.txt for information and build instructions.
//
// Note: START and END tags are used in comments to define sections used in
// tutorials. They are not part of the syntax for Protocol Buffers.
//
// To get an in-depth walkthrough of this file and the related examples, see:
// https://developers.google.com/protocol-buffers/docs/tutorials // [START declaration]
syntax = "proto3";
package tutorial;
// [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 = ;
int32 id = ; // Unique ID number for this person.
string email = ; enum PhoneType {
MOBILE = ;
HOME = ;
WORK = ;
} message PhoneNumber {
string number = ;
PhoneType type = ;
} repeated PhoneNumber phones = ;
} // Our address book file is just one of these.
message AddressBook {
repeated Person people = ;
}
// [END messages]

addressbook.proto

使用编译好的protoc.exe 通过addressbook.proto 生成CPP文件

指令如下  protoc -I=proto文件所在路径 -I=protobuf下src所在目录 --cpp_out=指定生成文件的路径  addressbook.proto(完整路径)

5 VC创建一个命令行工程 根目录下创建lib文件夹

将步骤4生成的lib文件拷贝到该文件夹

根目录下创建include文件夹

将protobuf->src下的google文件夹拷贝到该文件夹下

6 VC配置工程的属性

添加文件夹和lib文件夹 以及lib的文件名

如图

工程需要加入生成的protobuf CC文件

关闭预编译头

关闭 sdl检测

7 编译代码 代码内容可参考

protobuf->examples->add_person.cc

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

 #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 ;
}

add_person.cc

运行结果如图

google protobuf VC下的使用笔记的更多相关文章

  1. google::protobuf 编译方法

    这两天用了一下Protobuf 感觉很方便, 记录一下编译过程, 以做务忘(需要安装cmake): 1: 下载地址: https://developers.google.com/protocol-bu ...

  2. 《Dotnet9》系列-Google ProtoBuf在C#中的简单应用

    时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...

  3. google protobuf学习笔记:windows下环境配置

    欢迎转载,转载请注明原文地址:http://blog.csdn.net/majianfei1023/article/details/45371743 protobuf的使用和原理,请查看:http:/ ...

  4. window下编译并使用google protobuf

    参考网址: http://my.oschina.net/chenleijava/blog/261263 http://www.ibm.com/developerworks/cn/linux/l-cn- ...

  5. 在C语言环境下使用google protobuf

    本文写给经常使用C编程且不喜欢C++而又要经常使用google protobuf的人.        经常写通讯程序的人对数据进行序列化或者反序列化时,可能经常使用google的protobuf(PB ...

  6. Windows环境下google protobuf入门

    我使用的是最新版本的protobuf(protobuf-2.6.1),编程工具使用VS2010.简单介绍下google protobuf: google protobuf 主要用于通讯,是google ...

  7. VS下使用Google Protobuf完成SOCKET通信

    如何在Windows环境下的VS中安装使用Google Protobuf完成SOCKET通信 出处:如何在Windows环境下的VS中安装使用Google Protobuf完成SOCKET通信 最近一 ...

  8. 《精通并发与Netty》学习笔记(05 - Google Protobuf与Netty的结合)

    protobuf是由Google开发的一套对数据结构进行序列化的方法,可用做通信协议,数据存储格式,等等.其特点是不限语言.不限平台.扩展性强 Netty也提供了对Protobuf的天然支持,我们今天 ...

  9. 《精通并发与Netty》学习笔记(04 - Google Protobuf介绍)

    一 .Google Protobuf 介绍 protobuf是google团队开发的用于高效存储和读取结构化数据的工具,是Google的编解码技术,在业界十分流行,通过代码生成工具可以生成不同语言版本 ...

随机推荐

  1. html5-websocket实现基于远程方法调用的数据交互

    html5-websocket实现基于远程方法调用的数据交互   一般在传统网页中注册用户信息都是通过post或ajax提交到页面处理,到了HTML5后我们有另一种方法就是通过websocket进行数 ...

  2. 我是一个录像机(NVR)

    我是一个网络录像机,简称NVR.我的前辈是DVR,我们的区别很简单,DVR接的是模拟摄像机,我连接的是IP摄像机. 我的前辈DVR比我辛苦,因为模拟摄像机的模拟信号连过来之后,他要进行数字化.编码压缩 ...

  3. 5.IAP - FLASH

    一.Flash与时钟系统的关系            STM32系统时钟:                 HSE 高速外部时钟,电路上焊接的外部时钟,一般是4Mhz-16Mhz,板子上的是8Mhz ...

  4. PAT 甲级 1041 Be Unique (20 分)

    1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is desi ...

  5. c# 多态 虚方法

    多态: 为了解决同一种指令,可以有不同行为和结果 在运行时,可以通过调用同一个方法,来实现派生类中不同表现. 虚方法——抽象类——抽象函数——接口 虚方法: 被virtual 关键字修饰的方法  叫做 ...

  6. SyntaxError: 'ascii' codec can't decode byte 0xe4 in position 7: ordinal not in range(128)

    问题描述: SyntaxError: 'ascii' codec can't decode byte 0xe4 in position 7: ordinal not in range(128) 解决方 ...

  7. win10 64位,家庭版,C++,ini配置说明

      #include<windows.h> #include<iostream> #include <atlstr.h> using namespace std; ...

  8. RMI(远程方法调用)入门

    这两篇可以入门 http://www.cnblogs.com/ninahan0419/archive/2009/06/25/javarmi.html http://www.cnblogs.com/wx ...

  9. python 对象转字典

    从数据库中取出的数数据是 对象类型的,不能直接展示出来,需要转成字典类型,然后转成json 字符串,传给前端: data = {} data.update(obj.__dict__) print(da ...

  10. 一次sendmsg的改造过程

    比较蛋疼的一个改造过程,简单记录一下. 场景:用户态使用sendmsg发包,tcp报文,由于内核实现过程中存在一次kernel_read,也就是存在将pagecache中的内容拷贝一次的问题. 为了减 ...