zoukankan      html  css  js  c++  java
  • grpc使用记录(一) gRPC编译(mscv/gcc)

    1、编译前的准备工作

    1、下载源码

    git clone https://github.com/grpc/grpc.git
    

    2、下载依赖项,grpc的依赖项都以子模块的方式记录在third_party目录下,所以直接同步子模块即可。

    git submodule update --init
    # 可以使用 --recursive 选项递归子模块
    

    文件数比较多,完整下载后打包大小有804MB(PS.都是完整克隆)。

    文件列表

    3、安装go

    如果不按照go,则在生成编译脚本的时候会报以下错误:

    CMake Error at third_party/boringssl/CMakeLists.txt:38 (message):
      Could not find Go
    

    Windows上直接下载安装包 go1.12.5.windows-amd64 安装即可。

    Linux下也可以直接下载安装包安装,或者是使用相应发行版的包管理器安装。

    4、安装perl

    2、Windows下使用VS2019编译

    编译过程很简单,就是编译的东西比较多,速度比较慢。下面记录下编译的具体过程。

    2.1、使用cmake生成VS2019解决方案

    在生成VS2015解决方案前需要先安装cmake 3.x工具,已经VS开发环境。我这里是使用VS2019进行的编译,但也适用于VS2015/VS2017,这是执行cmake命令行工具的时候指定的参数稍有不同。


    2.1.1、生成时使用的基本选项设定

    指定编译平台工具集和编译配置类型等

    # 下面-G/-A 选项仅适用于VS2019,如果是VS2017或2015等,则需要使用-G "Visual Studio 15 2017 Win64"形式
    —G "Visual Studio 16 2019" -A x64 
    -DCMAKE_CONFIGURATION_TYPES=Release # 或者 DCMAKE_BUILD_TYPE=Release
    

    指定go.exe路径

    -DGO_EXECUTABLE="C:/Go/bin/go.exe"
    

    指定安装输出路径

    -DCMAKE_INSTALL_PREFIX="E:/complier/grpc/install" 
    # 下面几个也可以不指定
    -DINSTALL_BIN_DIR="E:/complier/grpc/install/grpc/bin" 
    -DINSTALL_MAN_DIR="E:/complier/grpc/install/grpc/share/man" 
    -DINSTALL_INC_DIR="E:/complier/grpc/install/grpc/include" 
    -DINSTALL_PKGCONFIG_DIR="E:/complier/grpc/install/grpc/share/pkgconfig" 
    

    指定使用静态运行时库

    -DgRPC_MSVC_STATIC_RUNTIME=1
    

    这里和下面编译第三方库时候都编译使用静态(MT)运行时的是对应的,如果下面编译第三方库都指定动态运行时库(MD),则无需指定该选项。


    2.1.2、仅使用上面选项,执行后报如下ZLIB警告。

    意思就是因为gRPC_ZLIB_PROVIDERmodule,所以强制设置gRPC_INSTALLFALSE

    CMake Warning at cmake/zlib.cmake:32 (message):
      gRPC_INSTALL will be forced to FALSE because gRPC_ZLIB_PROVIDER is "module"
    Call Stack (most recent call first):
      CMakeLists.txt:140 (include)
    
    

    解决这个警告需要使用到zlib库,关于它的编译这里就不再说了,可以查看 VS编译 x64版本zlib库 的内容。

    我这里就没有去编译它,直接使用了以前vcpkg安装的。然后指定如下选项继续。

     -DgRPC_ZLIB_PROVIDER=package 
     -DZLIB_LIBRARY_DEBUG="E:/complier/x64-windows-static/debug/lib/zlibd.lib" 
     -DZLIB_INCLUDE_DIR="E:/complier/x64-windows-static/include" 
     -DZLIB_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/zlib.lib" 
    

    2.1.3、继续,报如下CARES警告。这个警告可以不用管,也能正常编译通过。

    CMake Warning at cmake/cares.cmake:33 (message):
      gRPC_INSTALL will be forced to FALSE because gRPC_CARES_PROVIDER is
      "module"
    Call Stack (most recent call first):
      CMakeLists.txt:141 (include)
    
    

    但我这里还是解决它,先下载 c-ares 编译安装。

    # 下载源码(在grpc	hird_partycarescares目录下也是一样的,可以不用下载)
    git clone https://github.com/c-ares/c-ares.git
    # 生成VS工程
    cd c-ares
    # 注意,下面必须指定CARES_MSVC_STATIC_RUNTIME选项,否则后面编译grpc时候会通不过(找不到__imp__xxx)
    cmake . -G "Visual Studio 16 2019" -A x64 -DCMAKE_INSTALL_PREFIX=E:/complier/x64-windows-static -DCARES_STATIC=1 -DCARES_SHARED=0 -DCARES_MSVC_STATIC_RUNTIME=1
    # 编译
    msbuild /maxcpucount:4 /p:Configuration=Release /p:Platform=x64 c-ares.sln
    # 安装,上面生成VS工程时候指定了安装在当E:/complier/x64-windows-static目录下
    msbuild /maxcpucount:4 /p:Configuration=Release /p:Platform=x64 INSTALL.vcxproj
    

    然后指定下面几个选项后继续。

    -DgRPC_CARES_PROVIDER=package 
    -Dc-ares_DIR="E:/complier/x64-windows-static/lib/cmake/c-ares" # 这里是指定到c-ares-config.cmake文件的路径
    

    2.1.4、继续,报如下PROTOBUF警告

    -- 3.8.0.0
    CMake Warning (dev) at third_party/protobuf/cmake/install.cmake:60 (message):
      The file
      "E:/complier/grpc/grpc/third_party/protobuf/src/google/protobuf/stubs/io_win32.h"
      is listed in
      "E:/complier/grpc/grpc/third_party/protobuf/cmake/cmake/extract_includes.bat.in"
      but there not exists.  The file will not be installed.
    Call Stack (most recent call first):
      third_party/protobuf/cmake/CMakeLists.txt:231 (include)
    This warning is for project developers.  Use -Wno-dev to suppress it.
    
    CMake Warning at cmake/protobuf.cmake:51 (message):
      gRPC_INSTALL will be forced to FALSE because gRPC_PROTOBUF_PROVIDER is
      "module"
    Call Stack (most recent call first):
      CMakeLists.txt:142 (include)
    

    解决这个问题的步骤如下:

    1、进入grpc/third_party/protobuf目录,使用git命令更新下子模块git submodule update --init。或者在一开始对grpc更新子模块的时候,使用--recursive进行递归更新。

    2、生成编译配置,编译安装(参考:protocolbuffers/protobuf/appveyor.bat)

    # 参考protobuf目录下的 appveyor.bat 脚本文件
    mkdir build_msvc
    cd build_msvc
    # 指定 protobuf_BUILD_SHARED_LIBS 为0,以便生成静态库
    cmake -G "Visual Studio 14 2015 Win64" -Dprotobuf_BUILD_SHARED_LIBS=0 -Dprotobuf_UNICODE=1 -DCMAKE_INSTALL_PREFIX=E:/complier/x64-windows-static ../cmake
    
    # 编译,这里指定生成了Release版本的,默认是生成的Debug版本(实际上我两个版本都编译了)
    msbuild ALL_BUILD.vcxproj /maxcpucount:4 /p:Configuration=Release
    # 安装
    msbuild INSTALL.vcxproj /p:Configuration=Release
    

    3、指定下面几个选项后,继续使用cmake命令生成

    -DgRPC_CARES_PROVIDER="package" 
    -DProtobuf_INCLUDE_DIR="E:/complier/x64-windows-static/include"  
    -DProtobuf_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libprotobuf.lib" 
    -DProtobuf_LIBRARY_DEBUG="E:/complier/x64-windows-static/lib/libprotobufd.lib" 
    -DProtobuf_LITE_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libprotobuf-lite.lib" 
    -DProtobuf_LITE_LIBRARY_DEBUG="E:/complier/x64-windows-static/lib/libprotobuf-lited.lib" 
    -DProtobuf_PROTOC_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libprotoc.lib" 
    -DProtobuf_PROTOC_LIBRARY_DEBUG="E:/complier/x64-windows-static/lib/libprotocd.lib" 
    -DProtobuf_PROTOC_EXECUTABLE="E:/complier/x64-windows-static/bin/protoc.exe" 
    -DProtobuf_SRC_ROOT_FOLDER="third_party/protobuf" 
    

    2.1.5、继续,报SSL警告

    CMake Warning at cmake/ssl.cmake:37 (message):
      gRPC_INSTALL will be forced to FALSE because gRPC_SSL_PROVIDER is "module"
    Call Stack (most recent call first):
      CMakeLists.txt:143 (include)
    

    这里可以忽略它,不使用SSL即可。也可以编译openssl库,然后指定以下选项后,继续执行cmake。

    -DgRPC_SSL_PROVIDER="package" 
    -DOPENSSL_INCLUDE_DIR="E:/complier/x64-windows-static/include" 
    -DSSL_EAY_DEBUG="E:/complier/x64-windows-static/debug/lib/ssleay32.lib" 
    -DLIB_EAY_LIBRARY_DEBUG="E:/complier/x64-windows-static/debug/lib/libeay32.lib" 
    -DLIB_EAY_RELEASE="E:/complier/x64-windows-static/lib/libeay32.lib" 
    -DLIB_EAY_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libeay32.lib" 
    -DLIB_EAY_DEBUG="E:/complier/x64-windows-static/debug/lib/libeay32.lib" 
    -DSSL_EAY_LIBRARY_DEBUG="E:/complier/x64-windows-static/debug/lib/ssleay32.lib" 
    -DSSL_EAY_RELEASE="E:/complier/x64-windows-static/lib/ssleay32.lib" 
    -DSSL_EAY_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/ssleay32.lib" 
    

    编译openssl也很简单,可以参考 VS2015编译Openssl-1.1.0f 的内容,也可以直接使用vcpkg安装。

    我这里没有编译了,直接使用vcpkg安装后,备份出来后使用。

    vcpkg install openssl:x64-windows-static
    vcpkg export openssl:x64-windows-static --zip --output=openssl.x64-windows-static
    

    2.1.6、VS2015环境下可能遇到的错误

    下面两个错误,是我在指定编译器为VS2015的时候遇到的,VS2019并没有遇到。

    1、继续,报错误INTTYPES_FORMAT没有设置

    CMake Error at third_party/gflags/CMakeLists.txt:286 (message):
      Do not know how to define a 32-bit integer quantity on your system! Neither
      uint32_t, u_int32_t, nor __int32 seem to be available.  Set
      [GFLAGS_]INTTYPES_FORMAT to either C99, BSD, or VC7 and try again.
    
    

    这里可以打开grpc/third_party/gflags/CMakeLists.txt 查看

    gflagsCMakeLists

    这里在cmake命令参数中添加一个变量,指定使用C99或者VC7格式。

    -DINTTYPES_FORMAT:STRING=C99
    

    2、继续,报benchmark相关错误

    -- git Version: v1.4.0-e776aa02-dirty
    -- Version: 1.4.0
    -- Performing Test HAVE_STD_REGEX -- failed to compile
    -- Performing Test HAVE_GNU_POSIX_REGEX -- failed to compile
    -- Performing Test HAVE_POSIX_REGEX -- failed to compile
    CMake Error at third_party/benchmark/CMakeLists.txt:229 (message):
      Failed to determine the source files for the regular expression backend
    

    我直接打开grpc/third_party/benchmark/CMakeLists.txt做了如下修改,然后继续执行cmake,就通过了

    benchmarkCMakeLists


    完整的cmake执行命令如下:

    cmake —G "Visual Studio 16 2019" -DCMAKE_CONFIGURATION_TYPES=Release  -DGO_EXECUTABLE="C:/Go/bin/go.exe" -DCMAKE_INSTALL_PREFIX="E:/complier/grpc/install" -DgRPC_MSVC_STATIC_RUNTIME=1 -DgRPC_ZLIB_PROVIDER=package -DZLIB_LIBRARY_DEBUG="E:/complier/x64-windows-static/debug/lib/zlibd.lib" -DZLIB_INCLUDE_DIR="E:/complier/x64-windows-static/include" -DZLIB_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/zlib.lib" -DgRPC_CARES_PROVIDER=package  -Dc-ares_DIR="E:/complier/x64-windows-static/lib/cmake/c-ares" -DgRPC_PROTOBUF_PROVIDER="package" -DProtobuf_INCLUDE_DIR="E:/complier/x64-windows-static/include"  -DProtobuf_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libprotobuf.lib" -DProtobuf_LIBRARY_DEBUG="E:/complier/x64-windows-static/lib/libprotobufd.lib" -DProtobuf_LITE_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libprotobuf-lite.lib" -DProtobuf_LITE_LIBRARY_DEBUG="E:/complier/x64-windows-static/lib/libprotobuf-lited.lib" -DProtobuf_PROTOC_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libprotoc.lib" -DProtobuf_PROTOC_LIBRARY_DEBUG="E:/complier/x64-windows-static/lib/libprotocd.lib" -DProtobuf_PROTOC_EXECUTABLE="E:/complier/x64-windows-static/bin/protoc.exe" -DProtobuf_SRC_ROOT_FOLDER="third_party/protobuf" -DgRPC_SSL_PROVIDER="package" -DOPENSSL_INCLUDE_DIR="E:/complier/x64-windows-static/include" -DSSL_EAY_DEBUG="E:/complier/x64-windows-static/debug/lib/ssleay32.lib" -DLIB_EAY_LIBRARY_DEBUG="E:/complier/x64-windows-static/debug/lib/libeay32.lib" -DLIB_EAY_RELEASE="E:/complier/x64-windows-static/lib/libeay32.lib" -DLIB_EAY_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libeay32.lib" -DLIB_EAY_DEBUG="E:/complier/x64-windows-static/debug/lib/libeay32.lib" -DSSL_EAY_LIBRARY_DEBUG="E:/complier/x64-windows-static/debug/lib/ssleay32.lib" -DSSL_EAY_RELEASE="E:/complier/x64-windows-static/lib/ssleay32.lib" -DSSL_EAY_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/ssleay32.lib" -DINTTYPES_FORMAT:STRING=C99  ..grpc
    

    2.2、使用msbuild工具进行编译

    直接使用VS2019命令行工具进入生成的工程目录,执行下面命令即可

    msbuild /maxcpucount:4 /p:Configuration=Release ALL_BUILD.vcxproj
    

    编译耗时会比较久。

    注意,上面编译第三方库的时候,一定要指定使用使用静态运行时(/MT),否则可能这里编译完链接会出错。或者可以干脆不编译静态库,全部编译为动态库,使用默认的(/MD)参数。

    3、linux下编译

    这里只做了CentOS7下的编译,如果是其他发行版应该更简单。

     uname -a
    Linux localhost.localdomain 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
    

    下面很多操作需要root权限,下面的命令没有使用sudo,需要根据情况添加。

    3.1 CentO S下基本编译环境安装

    这里主要是安装以下gcc编译器和cmake等工具。

    # 安装cmake3、make(默认cmake版本太低)
    yum -y install epel-release # 默认情况下是没有cmake3的,必须安装epel(企业版 Linux 附加软件包)源才行
    yum install cmake3 make
    # 配置SLC源
    yum install centos-release-scl-rh centos-release-scl
    yum check-update
    # 安装devtoolset,因为默认安装gcc版本为4.8.5,版本太低
    yum install devtoolset-8-gcc devtoolset-8-gcc-c++
    # 安装完成之后执行下面语句,把相关环境变量导入
    source /opt/rh/devtoolset-4/enable
    # 也可以把上面文件的内容追加到/etc/profile中,就不用每次都要导入了
    # cat /opt/rh/devtoolset-4/enable >> /etc/profile
    # 也可以把这个文件放入 /etc/profile.d 目录下
    # ln -s /opt/rh/devtoolset-8/enable /etc/profile.d/devtoolset-8.sh
    
    # 安装go,也可直接下载安装包
    yum install go-toolset-7-golang
    # 启用go,这里和上面一样,也可以直接写到/etc/profile里面去
    source /opt/rh/go-toolset-7/enable
    

    devtoolset-8

    3.2 编译三方依赖库

    1、编译安装zlib

    cd grpc/third_party/zlib
    # 编译安装
    ./configure --prefix=/usr
    make && make install
    

    2、编译安装c-ares

    git clone https://github.com/c-ares/c-ares.git
    cd c-ares
    # 可以不下载,直接  cd grpc/third_party/cares/cares
    cmake3 . -DCMAKE_INSTALL_PREFIX=/usr
    make && make install
    

    3、编译安装protobuf

    # 进入grpc下源码目录,或者下载 git clone https://github.com/protocolbuffers/protobuf.git
    cd grpc/third_party/protobuf
    # 同步下子模块
    git submodule update --init --recursive
    # 创建cmake构建目录
    mkdir build && cd build
    # 生成Makefile
    cmake3 -DCMAKE_INSTALL_PREFIX=/usr ../cmake
    # 编译安装
    make -j4
    make install
    

    4、编译安装openssl

    这个也可以不安装的,不影响编译。

    # 下载安装包
    wget https://www.openssl.org/source/openssl-1.0.2s.tar.gz
    tar -xzf openssl-1.0.2s.tar.gz
    cd openssl-1.0.2s
    # 配置编译参数
    ./config --prefix=/usr
    # 编译安装
    make -j4
    make install
    

    注意,因为我上面编译的几个库都是安装到/usr目录的,所以下面cmake参数中没有再指定。如果是安装到自定义的目录的,则需要像上面Windows下编译一样,指定相关的路径。

    3.3编译grpc

    这里没什么直接使用

    # 创建cmake构建目录
    mkdir build && cd build
    # 执行cmake命令,生成Makefile脚本
    cmake3 ../grpc -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/home/pure/grpc/install -DgRPC_ZLIB_PROVIDER=package -DgRPC_CARES_PROVIDER=package -DgRPC_PROTOBUF_PROVIDER=package -DgRPC_SSL_PROVIDER=package
    # 编译
    make -j4
    

    builddone

  • 相关阅读:
    mysql索引
    mysql视图
    pymysql
    web前端基础
    【BZOJ2002】[HNOI2010] 弹飞绵羊(大力分块)
    【BZOJ2730】[HNOI2012] 矿场搭建(找割点)
    网络流(一)——最大流
    欧拉路与欧拉回路
    扫描线(一)——求矩形面积并
    【洛谷3396】哈希冲突(大力分块)
  • 原文地址:https://www.cnblogs.com/oloroso/p/11121922.html
Copyright © 2011-2022 走看看