ProtoBuf c++ 使用

以下针对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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值