window 10 Protocol v3.3.0 版本编译即使用

1.下载cmake:
https://cmake.org/download/
cmake-3.9.0-win64-x64.msi 最新版本

  1. 下载protobuf 3.3 版本
    https://github.com/google/protobuf/releases

  2. 安装Cmake

  3. 这里写图片描述

    注意:这里一定要选择你机器安装的版本,否则报以下错误
    错误1:CMake编译时出现“error in configuration process project files may be invalid”
    错误2:No CMAKE_CXX_COMPILER could be found.

    5.这里写图片描述

6.编译protobuf工程
在debug目录下生成三个文件 libprotobufd.lib,libprotobuf-lited.lib,libprotocd.lib

  1. proto 文件使用
syntax = "proto3";

message Friend {
    string name = 1;
    string number = 2;
}

message UserInfo {
  string nickname = 1;
  int32 age = 2;
  bool sex = 3;
  repeated Friend friends = 4;
  map<int32, string> skills = 5;
}
  1. 编译proto文件的bat文件
echo on
::set DST_DIR = %~dp0
set Path=protoc.exe
%Path% --cpp_out=%~dp0 test.proto

pause
  1. 程序调用

#include "stdafx.h"
#include<iostream>

#include "test.pb.h"
#include   <map> 

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    UserInfo searchA;
    searchA.set_nickname("East.Su");
    searchA.set_sex(true);
    for (int i = 0; i < 3; i++)
    {
        Friend* f1 = searchA.add_friends();
        f1->set_name("wangqiang");
        f1->set_number("1358946656");
    }

    auto map = searchA.mutable_skills();
    int key = 1;
    string val = "skill1";  
    (*map)[key] = val;

    key = 2;
    val = "skill2";
    (*map)[key] = val;


    searchA.SerializeAsString();

    UserInfo* searchB = new UserInfo();
    searchB->ParseFromString(searchA.SerializeAsString());

    cout << "nickname = " << searchB->nickname();

    // 数组的使用
    int size = searchB->friends_size();
    for (int i = 0; i<size; i++)
    {
        Friend tFriend = searchB->friends(i);
        string name = tFriend.name();
        string number = tFriend.number();
        cout << "frined: " << name << " = " << number << endl;
    }
    //map是的使用
    searchB->mutable_skills();
    auto pMapB = searchB->mutable_skills();
    google::protobuf::Map<google::protobuf::int32, std::string> * mapB = pMapB;
    if (mapB->size() > 0)
    {
        google::protobuf::Map<google::protobuf::int32, std::string>::iterator   it = mapB->begin();
        for (; it != mapB->end(); ++it)
        {
            cout << "skill: " << it->first << " = " << it->second << endl;
        }

    }

    getchar();
    return 0;
}

protobuf 3.3.0 any的使用:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值