grpc使用记录(一) gRPC编译(mscv/gcc)

一、编译前的准备工做

一、下载源码linux

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

二、下载依赖项,grpc的依赖项都以子模块的方式记录在third_party目录下,因此直接同步子模块便可。c++

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

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

文件列表

三、安装gogithub

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

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

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

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

四、安装perlcentos

二、Windows下使用VS2019编译

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

2.一、使用cmake生成VS2019解决方案

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


2.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.二、仅使用上面选项,执行后报以下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.三、继续,报以下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\third_party\cares\cares目录下也是同样的,能够不用下载)
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.四、继续,报以下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)

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

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

二、生成编译配置,编译安装(参考: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

三、指定下面几个选项后,继续使用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.五、继续,报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.六、VS2015环境下可能遇到的错误

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

一、继续,报错误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

二、继续,报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.二、使用msbuild工具进行编译

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

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

编译耗时会比较久。

注意,上面编译第三方库的时候,必定要指定使用使用静态运行时(/MT),不然可能这里编译完连接会出错。或者能够干脆不编译静态库,所有编译为动态库,使用默认的(/MD)参数。

三、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等工具。

# 安装cmake三、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

# 安装go,也可直接下载安装包
yum install go-toolset-7-golang
# 启用go,这里和上面同样,也能够直接写到/etc/profile里面去
source /opt/rh/go-toolset-7/enable

devtoolset-8

3.2 编译三方依赖库

一、编译安装zlib

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

二、编译安装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

三、编译安装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

四、编译安装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

相关文章
相关标签/搜索