获取Protocol Buffer

一. 获取Protocol Buffer

1.1 获得源码

Github:ProtocolBuffer源码

  1. Git clone之:git clone https://github.com/google/protobuf.git

  2. 或者直接下载release版本:https://github.com/google/protobuf/releases

  3. 本例选择从release下载3.7.0版本

  4. 下载并解压到目录: D:\wzx\protobuf\protobuf-3.7.0

1.2 编译protoc.exe(Windows平台)

准备工具: VS2017(包含C++模块),

cmake-3.14.0-win64-x64

提取码:jwa7 

   1.打开cmake目录,运行cmakegui:

     D:\wzx\protobuf\cmake-3.14.0-win64-x64\bin\cmake-gui.exe

   2.配置选项:

  1. Source Code目录: D:/wzx/protobuf/protobuf-3.7.0/cmake

  2. build the binaries目录:D:/wzx/protobuf/cmake-3.14.0-win64-x64/bin/protobuf-build

  3.  菜单栏Tools->configure,选择generator:

    1. 本例使用VS2017 64位版本,所以选择:Visual Studio 15 2017

   4.点击finish,结果如下图:

       5.将列表中的protobuf_BUILD_SHARED_LIBS勾上, 然后generator。
        查看D:\wzx\protobuf\cmake-3.14.0-win64-x64\bin\protobuf-build目录,你会发现生成了一个VS工程。

  3.用VS2015 编译protobuf-build工程:

  1. 双击打开D:\wzx\protobuf\cmake-3.14.0-win64-x64\bin\protobuf-build\protobuf.sln

  2. 我们选择release来生成解决方案:

      

然后等待编译完成。。。速度有点慢,大概要花几分钟。

   3.最后编译成功:

      

    4.进入D:\wzx\protobuf\cmake-3.14.0-win64-x64\bin\protobuf-build\Release, 即可看到编译的结果.

二、简单使用

    1.创建一个xxx.proto文件,至于这个文件怎么写,请看链接

http://www.cnblogs.com/dkblog/archive/2012/03/27/2419010.html

    2.利用D:\wzx\protobuf\cmake-3.14.0-win64-x64\bin\protobuf-build\Release 目录下的protoc.exe运行以下命令生成.cc和.h文件

    protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/xxx.proto

    

$SRC_DIR:源地址 D:\wzx\protobuf\cmake-3.14.0-win64-x64\bin\protobuf-build\Release

$DST_DIR:目的地址 D:\wzx\protobuf\cmake-3.14.0-win64-x64\bin\protobuf-build\Release\h
$SRC_DIR:源地址 D:\wzx\protobuf\cmake-3.14.0-win64-x64\bin\protobuf-build\Release

3.执行之后h文件夹下面出现my.pb.cc和my.pb.h两个文件

    4.把my.pb.cc、my.pb.h和libprotobuf.dll、libprotoc.dll、libprotobuf.lib、libprotoc.lib两个动态库添加到vs工程即可

      期间遇到了两个问题:

      1.是否忘记了向源中添加“#include "pch.h"”? 

         解决方案:添加头文件的引用 “#include "pch.h“,注意一定要添加到所有头文件最上面

      2.无法解析的外部符号 "class google::protobuf::internal::ExplicitlyConstructed<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > google::protobuf::internal::fixed_address_empty_string

       解决方案:在添加的pch.h中添加#define PROTOBUF_USE_DLLS宏定义

      3.属性设置:

       

      4.inet_ntoa函数出现:'inet_ntoa': Use inet_ntop() or InetNtop() instead or define

       解决方案:文件的属性页----->c/c++--->常规,将SDL检查改为否

         5.dll动态库放到工程目录或者exe目录下

猜你喜欢

转载自www.cnblogs.com/limm21918/p/10607981.html