一、安裝
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;
}