windows配置和编译grpc

grpc简介

gRPC是Google开发的一种高性能、开源的远程过程调用(RPC)框架。它可以让客户端应用程序像调用本地服务一样轻松地调用远程服务,并提供了多种语言的支持,如C++、Java、Python、Go等。

gRPC使用Protocol Buffers作为数据格式,可以在不同的平台上进行应用程序之间的通信,支持多种编程语言和多种操作系统。它采用基于HTTP/2的协议,提供了高效、快速且可扩展的远程调用功能,并带有负载均衡、认证、监控等功能,方便用户管理和维护分布式系统。

gRPC可用于构建各种类型的分布式应用程序,如微服务、云原生应用程序、大规模Web应用程序、移动应用程序等场景。由于其高性能和可扩展性,越来越多的企业和组织开始采用gRPC来构建他们的应用程序和服务。

grpc下载

由于国内环境,grpc下载极其困难,grpc项目在github上,源码地址为https://github.com/grpc/grpc,我们可以通过如下命令克隆grpc源码进行编译

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

但是国内网络环境执行submodule update时会失败,所以可以用国内的代码管理工具gitee进行克隆。
注意,目前在Gitee上只能找到gRPC依赖的部分”官方”镜像仓库,网友提供的镜像仓库较旧,因而只能构造v1.34.0版本.通过上述指令可以将v1.34.0版本的gRPC代码下载到grpc目录.
我们选择一个稳定的分支进行克隆

  1. git clone -b v1.34.0 https://gitee.com/mirrors/grpc-framework.git grpc

克隆之后,我们进入grpc文件夹,修改.gitmodules内仓库地址,修改前.gitmodules内容是这样的

  1. [submodule "third_party/zlib"]
  2. path = third_party/zlib
  3. #url = https://github.com/madler/zlib
  4. url = https://gitee.com/mirrors/zlib.git
  5. # When using CMake to build, the zlib submodule ends up with a
  6. # generated file that makes Git consider the submodule dirty. This
  7. # state can be ignored for day-to-day development on gRPC.
  8. ignore = dirty
  9. [submodule "third_party/protobuf"]
  10. path = third_party/protobuf
  11. #url = https://github.com/google/protobuf.git
  12. url = https://gitee.com/local-grpc/protobuf.git
  13. [submodule "third_party/googletest"]
  14. path = third_party/googletest
  15. #url = https://github.com/google/googletest.git
  16. url = https://gitee.com/local-grpc/googletest.git
  17. [submodule "third_party/benchmark"]
  18. path = third_party/benchmark
  19. #url = https://github.com/google/benchmark
  20. url = https://gitee.com/mirrors/google-benchmark.git
  21. [submodule "third_party/boringssl-with-bazel"]
  22. path = third_party/boringssl-with-bazel
  23. #url = https://github.com/google/boringssl.git
  24. url = https://gitee.com/mirrors/boringssl.git
  25. [submodule "third_party/re2"]
  26. path = third_party/re2
  27. #url = https://github.com/google/re2.git
  28. url = https://gitee.com/local-grpc/re2.git
  29. [submodule "third_party/cares/cares"]
  30. path = third_party/cares/cares
  31. #url = https://github.com/c-ares/c-ares.git
  32. url = https://gitee.com/mirrors/c-ares.git
  33. branch = cares-1_12_0
  34. [submodule "third_party/bloaty"]
  35. path = third_party/bloaty
  36. #url = https://github.com/google/bloaty.git
  37. url = https://gitee.com/local-grpc/bloaty.git
  38. [submodule "third_party/abseil-cpp"]
  39. path = third_party/abseil-cpp
  40. #url = https://github.com/abseil/abseil-cpp.git
  41. url = https://gitee.com/mirrors/abseil-cpp.git
  42. branch = lts_2020_02_25
  43. [submodule "third_party/envoy-api"]
  44. path = third_party/envoy-api
  45. #url = https://github.com/envoyproxy/data-plane-api.git
  46. url = https://gitee.com/local-grpc/data-plane-api.git
  47. [submodule "third_party/googleapis"]
  48. path = third_party/googleapis
  49. #url = https://github.com/googleapis/googleapis.git
  50. url = https://gitee.com/mirrors/googleapis.git
  51. [submodule "third_party/protoc-gen-validate"]
  52. path = third_party/protoc-gen-validate
  53. #url = https://github.com/envoyproxy/protoc-gen-validate.git
  54. url = https://gitee.com/local-grpc/protoc-gen-validate.git
  55. [submodule "third_party/udpa"]
  56. path = third_party/udpa
  57. #url = https://github.com/cncf/udpa.git
  58. url = https://gitee.com/local-grpc/udpa.git
  59. [submodule "third_party/libuv"]
  60. path = third_party/libuv
  61. #url = https://github.com/libuv/libuv.git
  62. url = https://gitee.com/mirrors/libuv.git

gRPC的依赖是通过git的submodules来关联的,代码下载下来之后可以看到.gitmodules文件,内部的git仓库地址都需要替换成Gitee的,例如:

  1. [submodule "third_party/zlib"]
  2. path = third_party/zlib
  3. url = https://github.com/madler/zlib
  4. # When using CMake to build, the zlib submodule ends up with a
  5. # generated file that makes Git consider the submodule dirty. This
  6. # state can be ignored for day-to-day development on gRPC.
  7. ignore = dirty

使用了zlib,在Gitee上搜索其代码仓库为https://gitee.com/mirrors/zlib,可以使用如下指令clone:

  1. git clone https://gitee.com/mirrors/zlib.git

因而替换成:

  1. [submodule "third_party/zlib"]
  2. path = third_party/zlib
  3. #url = https://github.com/madler/zlib
  4. url = https://gitee.com/mirrors/zlib.git
  5. # When using CMake to build, the zlib submodule ends up with a
  6. # generated file that makes Git consider the submodule dirty. This
  7. # state can be ignored for day-to-day development on gRPC.
  8. ignore = dirty

通过这种方法可以找到部分依赖库的最新镜像仓库,但是有一些找不到最新的,例如protobuf等库,用户local-grpc提供了gRPC依赖的全部代码仓库,可以使用这些仓库(注意代码不是同步镜像,导致gRPC只能构造相应版本),其中protobuf链接为:

  1. https://gitee.com/local-grpc/protobuf.git

这里将.gitmodules修改为如下内容即可:

  1. [submodule "third_party/zlib"]
  2. path = third_party/zlib
  3. #url = https://github.com/madler/zlib
  4. url = https://gitee.com/mirrors/zlib.git
  5. # When using CMake to build, the zlib submodule ends up with a
  6. # generated file that makes Git consider the submodule dirty. This
  7. # state can be ignored for day-to-day development on gRPC.
  8. ignore = dirty
  9. [submodule "third_party/protobuf"]
  10. path = third_party/protobuf
  11. #url = https://github.com/google/protobuf.git
  12. url = https://gitee.com/local-grpc/protobuf.git
  13. [submodule "third_party/googletest"]
  14. path = third_party/googletest
  15. #url = https://github.com/google/googletest.git
  16. url = https://gitee.com/local-grpc/googletest.git
  17. [submodule "third_party/benchmark"]
  18. path = third_party/benchmark
  19. #url = https://github.com/google/benchmark
  20. url = https://gitee.com/mirrors/google-benchmark.git
  21. [submodule "third_party/boringssl-with-bazel"]
  22. path = third_party/boringssl-with-bazel
  23. #url = https://github.com/google/boringssl.git
  24. url = https://gitee.com/mirrors/boringssl.git
  25. [submodule "third_party/re2"]
  26. path = third_party/re2
  27. #url = https://github.com/google/re2.git
  28. url = https://gitee.com/local-grpc/re2.git
  29. [submodule "third_party/cares/cares"]
  30. path = third_party/cares/cares
  31. #url = https://github.com/c-ares/c-ares.git
  32. url = https://gitee.com/mirrors/c-ares.git
  33. branch = cares-1_12_0
  34. [submodule "third_party/bloaty"]
  35. path = third_party/bloaty
  36. #url = https://github.com/google/bloaty.git
  37. url = https://gitee.com/local-grpc/bloaty.git
  38. [submodule "third_party/abseil-cpp"]
  39. path = third_party/abseil-cpp
  40. #url = https://github.com/abseil/abseil-cpp.git
  41. url = https://gitee.com/mirrors/abseil-cpp.git
  42. branch = lts_2020_02_25
  43. [submodule "third_party/envoy-api"]
  44. path = third_party/envoy-api
  45. #url = https://github.com/envoyproxy/data-plane-api.git
  46. url = https://gitee.com/local-grpc/data-plane-api.git
  47. [submodule "third_party/googleapis"]
  48. path = third_party/googleapis
  49. #url = https://github.com/googleapis/googleapis.git
  50. url = https://gitee.com/mirrors/googleapis.git
  51. [submodule "third_party/protoc-gen-validate"]
  52. path = third_party/protoc-gen-validate
  53. #url = https://github.com/envoyproxy/protoc-gen-validate.git
  54. url = https://gitee.com/local-grpc/protoc-gen-validate.git
  55. [submodule "third_party/udpa"]
  56. path = third_party/udpa
  57. #url = https://github.com/cncf/udpa.git
  58. url = https://gitee.com/local-grpc/udpa.git
  59. [submodule "third_party/libuv"]
  60. path = third_party/libuv
  61. #url = https://github.com/libuv/libuv.git
  62. url = https://gitee.com/mirrors/libuv.git

使用如下指令拉取gRPC所有依赖:

  1. cd grpc
  2. git submodule update --init

编译grpc

CMake安装

为了编译grpc,我们需要下载cmake,cmake是一个跨平台编译工具,在之前我们编译protobuf时已经下载过了,这里再次介绍一下
CMake是一个跨平台的开源构建工具,可以用于自动化构建、测试和打包C++代码。与其他构建工具相比,CMake的优点之一是它的配置文件具有可读性和可维护性,而且支持多种编译器、操作系统和构建工具。

我们要在windows上生成protobuf对应的库,就需要用到cmake。
官方下载地址https://cmake.org/download/, 选择Windows Source下载。
由于官方下载较慢,可以去我的网盘下载
https://pan.baidu.com/s/1Yg9Usdc3T-CYhyr9GiePCw?pwd=ng6x

验证码ng6x

NASM

编译grpc需要准备nasm,所以也要下载nasm, 下载地址为https://www.nasm.us/,点击下载指定版本,安装即可,记得安装后把安装的路径配置在环境变量里,我的装载了C盘
https://cdn.llfc.club/1685268741914.jpg
环境变量配置如下
https://cdn.llfc.club/1685268920381.jpg

有部分博主说要安装go和perl,我感觉不需要,但是我的电脑里确实有开发环境,这里还是告诉大家如何安装go和Perl

Go安装

windows环境下下载go,地址https://studygolang.com/dl
选择我们对应系统的安装包下载即可。一路下载安装,最后go会自动写入我们的环境变量。
Linux可以选择手动安装二进制包再配置环境变量。
安装好后,因为go的库包默认在外网,下载很慢,所以要切换代理

  1. go env -w GOPROXY=https://goproxy.cn,direct # 设置代理,国内网络开启

Perl安装

Perl安装可以去官网下载https://www.activestate.com/products/perl/,并按照提示一步步安装,也可以去网盘下载,直接安装,网盘地址https://pan.baidu.com/s/1i3GLKAp

编译grpc

接下来我们需要用CMake编译grpc来生成我们需要的visual studio 项目

https://cdn.llfc.club/1685271209281.jpg

1是grpc源码目录
2是grpc生成的visual studio项目目录
3 是我们要生成哪些项目。

我们先点击啊config进行配置,此时弹出对话框,我的visual studio版本是2019的, 所以我选择编译生成visual studio2019的工程。
接下来点击generate生成项目,大约几分钟可以生成,生成成功后我们点击open project打开工程,此时就是用visual sutido 2019打开grpc工程了。
接下来我们设置编译平台为64位,选择Debug模式,这里大家尽量编译Release版,Release版压缩的更好一点。
选择All项目进行全量编译,编译后就可以在Debug或Release文件夹找到对应生成的库文件和exe了。

总结

本文介绍了windows环境下如何配置grpc项目和编译生成我们需要的库。
如果大家还是不能顺利生成编译所需的库,可以用我的,这个源码在linux和windows都能用,源码包括第三方库都下载好了,网盘地址:
https://pan.baidu.com/s/1BBaAZ8-R-GSxxcy2s7TRWA?pwd=ybno
提取码:ybno

热门评论

热门文章

  1. 解密定时器的实现细节

    喜欢(566) 浏览(1794)
  2. C++ 类的继承封装和多态

    喜欢(588) 浏览(2503)
  3. slice介绍和使用

    喜欢(521) 浏览(1670)
  4. Linux环境搭建和编码

    喜欢(594) 浏览(5362)
  5. windows环境搭建和vscode配置

    喜欢(587) 浏览(1694)

最新评论

  1. 泛型算法的定制操作 secondtonone1:lambda和bind是C11新增的利器,善于利用这两个机制可以极大地提升编程安全性和效率。
  2. 基于锁实现线程安全队列和栈容器 secondtonone1:我是博主,你认真学习的样子的很可爱,哈哈,我画的是链表由空变成1个的情况。其余情况和你思考的类似,只不过我用了一个无效节点表示tail的指向,最初head和tail指向的都是这个节点。
  3. 解决博客回复区被脚本注入的问题 secondtonone1:走到现在我忽然明白一个道理,无论工作也好生活也罢,最重要的是开心,即使一份安稳的工作不能给我带来事业上的积累也要合理的舍弃,所以我还是想去做喜欢的方向。
  4. 利用内存模型优化无锁栈 卡西莫多的礼物:感谢博主指点,好人一生平安o(* ̄▽ ̄*)ブ
  5. 类和对象 陈宇航:支持!!!!

个人公众号

个人微信