zoukankan      html  css  js  c++  java
  • google protobuf VC下的使用笔记

    1

    使用protobuf 2.x 下载地址(3.x 在c++11 vs2017下报错)

    源码 https://github.com/google/protobuf

    或者直接下载 二进制文件

    2 如果下载的是代码 编译需要使用cmake 来生成VC的工程

    cmake的使用从略

    编译设置如图

    3 如果下载的是代码  开启VC工程编译protobuf  由于以后自写代码需要使用protobuf的LIB 所以编译时请确认编译的版本

    我使用的是VC2017编译的 x64 debug 版本

    如图 

    4 以protobuf中的例子 来进行实验

    addressbook.proto

    内容如下

     1 // See README.txt for information and build instructions.
     2 //
     3 // Note: START and END tags are used in comments to define sections used in
     4 // tutorials.  They are not part of the syntax for Protocol Buffers.
     5 //
     6 // To get an in-depth walkthrough of this file and the related examples, see:
     7 // https://developers.google.com/protocol-buffers/docs/tutorials
     8 
     9 // [START declaration]
    10 syntax = "proto3";
    11 package tutorial;
    12 // [END declaration]
    13 
    14 // [START java_declaration]
    15 option java_package = "com.example.tutorial";
    16 option java_outer_classname = "AddressBookProtos";
    17 // [END java_declaration]
    18 
    19 // [START csharp_declaration]
    20 option csharp_namespace = "Google.Protobuf.Examples.AddressBook";
    21 // [END csharp_declaration]
    22 
    23 // [START messages]
    24 message Person {
    25   string name = 1;
    26   int32 id = 2;  // Unique ID number for this person.
    27   string email = 3;
    28 
    29   enum PhoneType {
    30     MOBILE = 0;
    31     HOME = 1;
    32     WORK = 2;
    33   }
    34 
    35   message PhoneNumber {
    36     string number = 1;
    37     PhoneType type = 2;
    38   }
    39 
    40   repeated PhoneNumber phones = 4;
    41 }
    42 
    43 // Our address book file is just one of these.
    44 message AddressBook {
    45   repeated Person people = 1;
    46 }
    47 // [END messages]
    addressbook.proto

    使用编译好的protoc.exe 通过addressbook.proto 生成CPP文件

    指令如下  protoc -I=proto文件所在路径 -I=protobuf下src所在目录 --cpp_out=指定生成文件的路径  addressbook.proto(完整路径)

     

    5 VC创建一个命令行工程 根目录下创建lib文件夹 

    将步骤4生成的lib文件拷贝到该文件夹

    根目录下创建include文件夹

    将protobuf->src下的google文件夹拷贝到该文件夹下

    6 VC配置工程的属性

    添加文件夹和lib文件夹 以及lib的文件名

    如图

     工程需要加入生成的protobuf CC文件

    关闭预编译头

    关闭 sdl检测

     7 编译代码 代码内容可参考

    protobuf->examples->add_person.cc

     1 // See README.txt for information and build instructions.
     2 
     3 #include <iostream>
     4 #include <fstream>
     5 #include <string>
     6 #include "addressbook.pb.h"
     7 using namespace std;
     8 
     9 // This function fills in a Person message based on user input.
    10 void PromptForAddress(tutorial::Person* person) {
    11   cout << "Enter person ID number: ";
    12   int id;
    13   cin >> id;
    14   person->set_id(id);
    15   cin.ignore(256, '
    ');
    16 
    17   cout << "Enter name: ";
    18   getline(cin, *person->mutable_name());
    19 
    20   cout << "Enter email address (blank for none): ";
    21   string email;
    22   getline(cin, email);
    23   if (!email.empty()) {
    24     person->set_email(email);
    25   }
    26 
    27   while (true) {
    28     cout << "Enter a phone number (or leave blank to finish): ";
    29     string number;
    30     getline(cin, number);
    31     if (number.empty()) {
    32       break;
    33     }
    34 
    35     tutorial::Person::PhoneNumber* phone_number = person->add_phones();
    36     phone_number->set_number(number);
    37 
    38     cout << "Is this a mobile, home, or work phone? ";
    39     string type;
    40     getline(cin, type);
    41     if (type == "mobile") {
    42       phone_number->set_type(tutorial::Person::MOBILE);
    43     } else if (type == "home") {
    44       phone_number->set_type(tutorial::Person::HOME);
    45     } else if (type == "work") {
    46       phone_number->set_type(tutorial::Person::WORK);
    47     } else {
    48       cout << "Unknown phone type.  Using default." << endl;
    49     }
    50   }
    51 }
    52 
    53 // Main function:  Reads the entire address book from a file,
    54 //   adds one person based on user input, then writes it back out to the same
    55 //   file.
    56 int main(int argc, char* argv[]) {
    57   // Verify that the version of the library that we linked against is
    58   // compatible with the version of the headers we compiled against.
    59   GOOGLE_PROTOBUF_VERIFY_VERSION;
    60 
    61   if (argc != 2) {
    62     cerr << "Usage:  " << argv[0] << " ADDRESS_BOOK_FILE" << endl;
    63     return -1;
    64   }
    65 
    66   tutorial::AddressBook address_book;
    67 
    68   {
    69     // Read the existing address book.
    70     fstream input(argv[1], ios::in | ios::binary);
    71     if (!input) {
    72       cout << argv[1] << ": File not found.  Creating a new file." << endl;
    73     } else if (!address_book.ParseFromIstream(&input)) {
    74       cerr << "Failed to parse address book." << endl;
    75       return -1;
    76     }
    77   }
    78 
    79   // Add an address.
    80   PromptForAddress(address_book.add_people());
    81 
    82   {
    83     // Write the new address book back to disk.
    84     fstream output(argv[1], ios::out | ios::trunc | ios::binary);
    85     if (!address_book.SerializeToOstream(&output)) {
    86       cerr << "Failed to write address book." << endl;
    87       return -1;
    88     }
    89   }
    90 
    91   // Optional:  Delete all global objects allocated by libprotobuf.
    92   google::protobuf::ShutdownProtobufLibrary();
    93 
    94   return 0;
    95 }
    add_person.cc

     运行结果如图

    作 者: itdef
    欢迎转帖 请保持文本完整并注明出处
    技术博客 http://www.cnblogs.com/itdef/
    B站算法视频题解
    https://space.bilibili.com/18508846
    qq 151435887
    gitee https://gitee.com/def/
    欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
    如果觉得不错,欢迎点赞,你的鼓励就是我的动力
    阿里打赏 微信打赏
  • 相关阅读:
    Java并发编程
    Git
    Spring Boot
    IDEA工具
    Java基础
    数据库架构
    设计模式
    网络基础
    管理知识
    linux安装数据库mysql
  • 原文地址:https://www.cnblogs.com/itdef/p/8522057.html
Copyright © 2011-2022 走看看