zoukankan      html  css  js  c++  java
  • window 安装 Protobuf

    环境安装

    1:下载CMake 

    2:打开VS Command Prompt

    3:修改工作目录到目标目录

    cd C:Path o

    4:创建编译完后 protobuf headers/libraries/binaries 将要安装的文件夹

     C:Path o>mkdir install

    5:确保 'cmake' 命令可用,(如果不可用确保 把它加入到 path 环境变量中)

    set PATH=%PATH%;D:Program Filescmake-3.5.2-win32-x86in

    6:确保Git命令可用(如果不可用,添加到到 path 环境变量)

    set PATH=%PATH%;D:Program FilesGitcmd

    源设置

    下载 packages https://github.com/google/protobuf/releases

    把protobuf 放入  C:Path o 目标

    cd  C:Path oprotobufcmake

    CMake 配置

    参考: Visual Studio Generators

    注意:64位请用对应的 64位VS命令行

    1:创建一个 build 目录,并且改变当前工作目录到build

    mkdir build & cd build

    ------创建Release配置

    C:Path oprotobufcmakeuild>mkdir release & cd release

    C:Path oprotobufcmakeuild elease>cmake -G "NMake Makefiles" ^
    -DCMAKE_BUILD_TYPE=Release ^
    -DCMAKE_INSTALL_PREFIX=../../../../install ^
    ../..

    ------创建Debug 配置

    C:Path oprotobufcmakeuild>mkdir debug & cd debug
    C:Path oprotobufcmakeuilddebug>cmake -G "NMake Makefiles" ^
    -DCMAKE_BUILD_TYPE=Debug ^
    -DCMAKE_INSTALL_PREFIX=../../../../install ^
    ../..

    -----创建Visual Studio 解决方案文件

    C:Path oprotobufcmakeuild>mkdir solution & cd solution
    C:Path oprotobufcmakeuildsolution>cmake -G "Visual Studio 11 2012 Win64" ^

    -DCMAKE_INSTALL_PREFIX=../../../../install ^
    -Dprotobuf_BUILD_TESTS=OFF ^
    ../..

    备注

    Generates Visual Studio 11 (VS 2012) project files.

    Visual Studio 11 2012 Win64  --Specify target platform x64.

    Visual Studio 11 2012 ARM   --Specify target platform ARM.

    Visual Studio 11 2012 <WinCE-SDK>  --Specify target platform matching a Windows CE SDK name.

    Generates Visual Studio 12 (VS 2013) project files:

    Visual Studio 12 2013 Win64  --Specify target platform x64.

    Visual Studio 12 2013 ARM     --Specify target platform ARM.

    Generates Visual Studio 14 (VS 2015) project files:

    Visual Studio 11 2012 Win64  --Specify target platform x64.

    Visual Studio 11 2012 ARM   --Specify target platform ARM.

    Visual Studio 11 2012 <WinCE-SDK>  --Specify target platform matching a Windows CE SDK name.

    编译

    To compile protobuf:

    C:Path oprotobufcmakeuild elease>nmake

    或者

    C:Path oprotobufcmakeuilddebug>nmake

    或者

    VS:打开生成的.sln 文件 即可。

    如果出现如下错误:

     

    修改Platform Toolset 即可

    安装

    To install protobuf to the specified *install* folder:

    C:Path oprotobufcmakeuild elease>nmake install

    or

    C:Path oprotobufcmakeuilddebug>nmake install

    或者编译VS解决方案中的“INSTALL”。

    如果出现编译错误,尝试用管理员权限打开VS重新试试

    定义消息体

    package tutorial;
    
    message Person {
      required string name = 1;
      required int32 id = 2;
      optional string email = 3;
    
      enum PhoneType {
        MOBILE = 0;
        HOME = 1;
        WORK = 2;
      }
    
      message PhoneNumber {
        required string number = 1;
        optional PhoneType type = 2 [default = HOME];
      }
    
      repeated PhoneNumber phone = 4;
    }
    
    message AddressBook {
      repeated Person person = 1;
    }

    编译生成对应library

    c++:(protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/addressbook.proto)

    protoc -I=. --cpp_out=. HookMessage.proto

     生成对应的 .h 和.cpp 文件

    c#:(protoc -I=$SRC_DIR --csharp_out=$DST_DIR $SRC_DIR/addressbook.proto)

    生成.cs 文件

     

    c++项目使用

    1:添加protobuf头文件:   protobuf下的Src (protobuf-3.0.0-beta-2src)

     Property-->Configuration Properties-->c/c++-->General:Additional Include Directories

    2:添加类库文件 (上面build出来的类库,如上例:C:Path oprotobufcmakeuilddebug)

     Property-->Configuration Properties-->Linker-->General-->Additional Library Directories

    在使用cpp文件顶部加上 

    #pragma comment(lib, "libprotobufd.lib")
    #pragma comment(lib, "libprotocd.lib")

    build项目:

    可能提示错误

    该错误又由于 生成的类库和当前的项目使用的是不一样的 Runtime Library

    修改如下配置即可: Property-->Configuration Properties-->c/c++-->Cide Generation-->Runtime Library(   Multi-threaded DLL (/MD)  |  Multi-threaded Debug (/MTd)  等)

    C#项目使用

    引用Google.Protobuf.dll        protobuf 源包中C#项目生成的DLL(需要打开项目自己编译生成)

  • 相关阅读:
    安全编码1
    VPP tips
    VPP概述汇总
    C语言安全编码摘录
    TCP-proxy
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.4. Matplotlib: plotting
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.3. NumPy: creating and manipulating numerical data
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.2. The Python language
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.1. Python scientific computing ecosystem
    25马5跑道,求最快的五匹马的需要比赛的次数
  • 原文地址:https://www.cnblogs.com/grayguo/p/5431856.html
Copyright © 2011-2022 走看看