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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章