请容我想一想
2022/03/24阅读:70主题:默认主题
在Win10上编译gRPC库
-
gRPC概述
gRPC (gRPC Remote Procedure Calls) 是Google发起的一个开源远程过程调用 (Remote procedure call) 系统。该系统基于 HTTP/2 协议传输,使用Protocol Buffers 作为接口描述语言。--摘抄自维基百科
-
gRPC库开源地址
Github:https://github.com/grpc/grpc
码云:https://gitee.com/mirrors/grpc
建议使用码云上的地址进行克隆,否则有可能克隆失败。
-
安装编译gRPC源码的工具
-
Visual Studio 2015以上版本(Visual C++ compiler)
Visual Studio IDE下载地址: https://visualstudio.microsoft.com/zh-hans/vs/older-downloads/
-
Git
Git工具下载地址:https://git-scm.com/
-
CMake
CMake下载地址:https://cmake.org/download/
-
nasm
nasm下载地址:https://www.nasm.us/
-
Ninja
Ninja下载地址:https://ninja-build.org/
-
说明:切记不可使用MinGW编译器,经验之谈(笔者使用Gcc编译器走了好多弯路),Windows平台上必须使用MSVC编译器。
-
在Windows平台上的构建步骤
-
从码云上克隆gRPC源码
git clone --recurse-submodules -b v1.42.0 https://gitee.com/mirrors/grpc
提示:在克隆过程中,gRPC源码依赖16个第三方库,由于国内不可抗力的网络环境可能会克隆失败。
-
进入到grpc的源码目录[cd E:\3rdLibrary\grpc]找到.gitmodules文件,更改内容如下
[submodule "third_party/zlib"]
path = third_party/zlib
url = https://gitee.com/mirrors/zlib.git
# When using CMake to build, the zlib submodule ends up with a
# generated file that makes Git consider the submodule dirty. This
# state can be ignored for day-to-day development on gRPC.
ignore = dirty
[submodule "third_party/protobuf"]
path = third_party/protobuf
url = https://gitee.com/mirrors/protobuf.git
[submodule "third_party/googletest"]
path = third_party/googletest
url = https://gitee.com/mirrors/googletest.git
[submodule "third_party/benchmark"]
path = third_party/benchmark
url = https://gitee.com/mirrors/benchmark.git
[submodule "third_party/boringssl-with-bazel"]
path = third_party/boringssl-with-bazel
url = https://gitee.com/mirrors/boringssl.git
[submodule "third_party/re2"]
path = third_party/re2
url = https://gitee.com/mirrors/re2.git
[submodule "third_party/cares/cares"]
path = third_party/cares/cares
url = https://gitee.com/mirrors/c-ares.git
branch = cares-1_12_0
[submodule "third_party/bloaty"]
path = third_party/bloaty
url = https://github.com.cnpmjs.org/google/bloaty.git
[submodule "third_party/abseil-cpp"]
path = third_party/abseil-cpp
url = https://gitee.com/mirrors/abseil-cpp.git
branch = lts_2020_02_25
[submodule "third_party/envoy-api"]
path = third_party/envoy-api
url = https://github.com.cnpmjs.org/envoyproxy/data-plane-api.git
[submodule "third_party/googleapis"]
path = third_party/googleapis
url = https://gitee.com/mirrors/googleapis.git
[submodule "third_party/protoc-gen-validate"]
path = third_party/protoc-gen-validate
url = https://gitee.com/mirrors/protoc-gen-validate.git
[submodule "third_party/libuv"]
path = third_party/libuv
url = https://gitee.com/mirrors/libuv.git
[submodule "third_party/opencensus-proto"]
path = third_party/opencensus-proto
url = https://github.com.cnpmjs.org/census-instrumentation/opencensus-proto.git
[submodule "third_party/opentelemetry"]
path = third_party/opentelemetry
url = https://github.com.cnpmjs.org/open-telemetry/opentelemetry-proto.git
[submodule "third_party/xds"]
path = third_party/xds
url = https://github.com.cnpmjs.org/cncf/xds.git
以上就是gRPC源码下包含的16个子模块,也是gRPC依赖的第三方库,gRPC在包含第三方库的开源地址都是在Github上,由于Github访问比较困难,故我将其子模块的下载地址更改为国内码云的地址或者github镜像站地址。 当然我们也可以进入到grpc\third_party目录下,进行每一个模块的克隆,但是要注意的是boringssl-with-bazel模块和cares模块。
submodule "third_party/boringssl-with-bazel
path = third_party/boringssl-with-bazel
url = https://gitee.com/mirrors/boringssl.git
submodule "third_party/cares/cares"
path = third_party/cares/cares
url = https://gitee.com/mirrors/c-ares.git
branch = cares-1_12_0
以上两个模块的路径,稍不注意就容易犯错,它们俩在grpc\third_party目录下的各自的子目录下。
-
克隆gRPC库的第三方依赖
git submodule sync
git submodule update --init
-
使用CMake工具,构建Makefile文件
cd %\grpc%(进入到grpc源码的路径)
//使用VS2015构建静态库
cmake -G "Visual Studio 14 2015" -DgRPC_INSTALL=ON -DgRPC_BUILD_TESTS=OFF ./CMakeLists.txt
//使用VS2017构建静态库
cmake -G "Visual Studio 15 2017" -DgRPC_INSTALL=ON -DgRPC_BUILD_TESTS=OFF ./CMakeLists.txt
//使用VS2019构建静态库
cmake -G "Visual Studio 16 2019" -DgRPC_INSTALL=ON -DgRPC_BUILD_TESTS=OFF ./CMakeLists.txt
//使用VS2017构建动态库
cmake -G "Visual Studio 15 2017" -DgRPC_INSTALL=ON -DgRPC_BUILD_TESTS=OFF -DBUILD_SHARED_LIBS=ON ./CMakeLists.txt
-
编译
//编译Debug版本
cmake --build . --config Debug
//编译Release版本
cmake --build . --config Release
-
编译grpc库成果如下
bin目录下的程序集如下:
-
gRPC学习相关网站
-
gRPC官网教程
https://grpc.io/
-
gRPC官方文档中文版
http://doc.oschina.net/grpc
-
作者介绍