一、安装
1、下载:https://github.com/protocolbuffers/protobuf/releases/tag/v2.6.1
2、解压。
3、进入vsprojects,双击 extract_includes.bat 生成 include 目录。
4、点击 protobuf.sln,对(libprotobuf、libprotobuf-lite、libprotoc)点击菜单的项目—属性—配置属性—C/C+±–预处理器—编辑—在最底部添加:_SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS。
5、右键 生成解决方案
二、使用
我资源中有相关文档,目前刚上传(1积分),在审核中。
protoc.exe 使用方法:
protoc --cpp_out=. test.proto
VS2015 使用说明:
属性->VC++ 目录->包含目录,包含 include 目录
属性->VC++ 目录->库目录,包含 lib 目录
将生成的 test.pb.h/test.pb.cc 添加到当前项目
test.proto 使用示例:
#include <fstream>
#include <iostream>
using namespace std;
#pragma comment(lib, "libprotobuf.lib")
#pragma comment(lib, "libprotoc.lib")
#include "hello.pb.h"
using namespace test;
int main() {
Person p;
p.set_name("test");
p.set_id(1);
p.set_email("a.iabc.com");
fstream output("./log", ios::out | ios::trunc | ios::binary);
if (!p.SerializeToOstream(&output)) {
cerr << "Failed to write msg." << endl;
return -1;
}
return 0;
}