Windows系统下编译grpc源码+VS2019配置使用grpc

Windows系统下编译grpc+VS2019配置使用grpc

导语: 本文记录了开源通信框架grpc在windows系统上编译的过程,以及使用VS 2019配置使用编译出来的grpc相关成果物展示demo.在window下编译grpc也是无奈啊,试过好多种办法(通过msys配置等),都不能为VS所用,所以只能开启一段旅程。 网上相关资料也有很多,这里基于grpc 和 VS 最新版本丰富下细节,做一下记录。

Step-1: 编译grpc

1-1 准备编译环境

(1) 安装Git
作用:下载grpc代码以及相关的第三方库代码。
参考:https://git-scm.com/
(2) 安装CMake
作用:用来编译
参考:https://cmake.org/download/
备注:将Cmake bin 目录配置到系统环境变量Path。
(3) 安装Perl
作用:grpc依赖的第三方库依赖的包。
参考:安装配置perl
(4) 安装Go
作用:grpc依赖的第三方库依赖的包。
参考:安装配置Go
(5) 安装yasm
作用:grpc依赖的第三方库依赖的包。
参考:安装配置yasm
备注:将vsyasm.exe所在目录配置到系统环境变量Path。
(6) 安装ninja
作用:grpc依赖的第三方库依赖的包。
参考:安装配置ninja
备注:将ninja.exe所在目录配置到系统环境变量Path。

这里面 perl go yasm ninja 可以用通过Choco(windows上的包管理工具安装),但是不推荐,除非你有梯子

1-2 下载grpc源码以及第三方依赖库源码

(1) 网速快或者有梯子的情况下,git bash下直接执行如下命令,然后耐心等待就可以了。

git clone https://github.com/grpc/grpc.git 
git submodule update  --init

安静的等待就行。
(2) 网速慢的情况下,git bash下直接执行如下命令

git clone https://gitclone.com/github.com/grpc/grpc.git
git submodule update --init

执行完上述命令,可能会卡住,别慌Ctrl+C退出即可。

cd third_party/
git clone  https://gitclone.com/github.com/abseil/abseil-cpp.git
git clone  https://gitclone.com/github.com/google/benchmark.git
git clone  https://gitclone.com/github.com/google/bloaty.git
git clone https://gitclone.com/github.com/google/boringssl.git
git clone https://gitclone.com/github.com/c-ares/c-ares.git
git clone https://gitclone.com/github.com/envoyproxy/data-plane-api.git
git clone https://gitclone.com/github.com/gflags/gflags.git
git clone https://gitclone.com/github.com/googleapis/googleapis.git
git clone https://gitclone.com/github.com/google/googletest.git
git clone https://gitclone.com/github.com/libuv/libuv.git
git clone https://gitclone.com/github.com/google/protobuf.git
git clone https://gitclone.com/github.com/envoyproxy/protoc-gen-validate.git
git clone https://gitclone.com/github.com/cncf/udpa.git
git clone https://gitclone.com/github.com/madler/zlib.git

如果个别库还是下载不下来,可以上github上用新的git url下载,或者直接下载代码包解压到相依目录。

注意下载完第三方库的源码后,有些目录需要调整到执行完git submodule update --init 一致的要求,否则Cmake的时候路径不对,例如:boringssl- -> boringssl-with-bazel /c-ares -->/cares/cares data-plane-api --> envoy-api

1-3 编译

(1) 修改编译脚本错误
由于历史原因,请更改grpc/cmake/ssl.cmake

set(_gRPC_SSL_INCLUDE_DIR ${BORINGSSL_ROOT_DIR}/src/include) --> set(_gRPC_SSL_INCLUDE_DIR ${BORINGSSL_ROOT_DIR}/include)

(2) 查询 cmake 支持的 VS 版本

cmake -G

在这里插入图片描述
(3) 修改 /grpc/third_party/zlib/gzguts.h

#ifdef _WIN32
#  include <stddef.h>
#pragma warning(disable:4996) // add this line
#endif

(3) 编译
lunch cmd ,cd 到代码根目录,执行如下命令。

mkdir .build
cd .build
cmake .. -G "Visual Studio 16 2019" -A x64 -DCMAKE_BUILD_TYPE=Release
cmake --build .

耐心等待编译结果, 过程中会有grpc.sln 生成,最后编译成功会生成grpc的相关lib,以及第三方的相关lib.

grpc相关lib 主要在.build/debug/目录下
在这里插入图片描述
第三方lib 主要在.build/third_party/xxxx/debug/
部分第三方lib 主要在.build/third_party/

Step-2: VS 2019 配置使用grpc

2-1 生成proto相关的C++文件

(1) VS2019解决方案下建立如下项目:
选择Debug – x64构建配置
在这里插入图片描述
其中helloworld.proto greeter_server.cc greeter_client.cc 可以从grpc/example/的相应目录下获取。
(2) 生成C++文件
lunch cmd & cd 到项目的proto文件夹下,运行如下命令

$ E:\workspace\01_code\vs2019\grpc\grpc\.build\third_party\protobuf\Debug\protoc.exe --grpc_out=. --plugin=protoc-gen-grpc="E:\workspace\01_code\vs2019\grpc\grpc\.build\Debug\grpc_cpp_plugin.exe" helloworld.proto
$ E:\workspace\01_code\vs2019\grpc\grpc\.build\third_party\protobuf\Debug\protoc.exe -I=. --cpp_out=. helloworld.proto

注意请用编译出来的exe文件生成C++ 文件,否则编译时会提示版本错误。

2-1 配置VS项目环境

(1) 配置include目录
项目右键–> 属性
在这里插入图片描述
(2) 配置lib包含目录
项目右键–> 属性
在这里插入图片描述
(2) 配置lib 项目名称
项目右键–> 属性
在这里插入图片描述
具体的lib库比较多,这里给出helloworld的清单。

libprotobufd.lib
grpc.lib
grpc++.lib
gpr.lib
zlibd.lib
cares.lib
address_sorting.lib
upb.lib
absl_str_format_internal.lib
absl_strings.lib
absl_strings_internal.lib
absl_cord.lib
absl_bad_optional_access.lib
absl_base.lib
absl_throw_delegate.lib
absl_time.lib
absl_time_zone.lib
absl_int128.lib
absl_raw_logging_internal.lib
crypto.lib
ssl.lib

3-1 编译执行

VS 上先编译执行 server端,再执行client端:
在这里插入图片描述
注意第一次启动server的时候会提示少zlibd.dll 库,到 ./build/third_party/zlib/XXX/debug/ 目录下找到相应的库复制到 server exe 文件相同目录就可以了。

结语:本文在形成过程中也参考很多之前网友的贡献,在此感谢。限于篇幅很多细节还是不能详细展开,但是大致脉络已经呈现,希望对大家能有所帮助。

  • 7
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值