您的位置:首页 > 编程语言 > C语言/C++

ProtoBuf c++ 使用

2016-11-24 16:03 363 查看
以下针对protobuf 2.6版本

下载:

https://github.com/google/protobuf/releases/download/v2.6.0/protobuf-2.6.0.zip

https://github.com/google/protobuf/releases/download/v2.6.0/protoc-2.6.0-win32.zip

1.解压protobuf-2.6.0.zip

编译 vsprojects/protobuf.sln

在vsprojects\Debug生成三个文件: libprotobuf.lib,libprotobuf-lite.lib,libprotoc.lib

2.拷贝protobuf-2.6.0/src/google到Code/common/ 下面,拷贝上面三个lib到你的工程目录某个文件夹下 面(这里我们放在tlib/Debug)。

3.配置文件



4.增加宏 _SCL_SECURE_NO_WARNINGS




5.编译环境搭建好后开始生成协议

*.proto文件

package com.game.Protobuf;
option optimize_for = LITE_RUNTIME;
message SGOMsMapInfo
{
required int32 mapid = 1;
}


c++使用代码:

#include "stdafx.h"
#include <vector>
#include <string.h>
#include <assert.h>
#include "GameServer.pb.h"
using namespace std;
using namespace com::game::Protobuf;
int main()
{
SGOMsMapInfo gs;
gs.set_mapid(1);
string strSend = "";
gs.SerializeToString(&strSend);
//gs.SerailzeToString(strSend);
string strAccept = strSend;
SGOMsMapInfo gsAccept;
if(gsAccept.ParseFromString(strAccept))  // 解析该字符串
{
printf("val :%d\n", gsAccept.mapid());
}
getchar();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  protobuf c++