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. vscode搭建windows C++开发环境

    喜欢(596) 浏览(78154)
  2. 聊天项目(28) 分布式服务通知好友申请

    喜欢(507) 浏览(5647)
  3. 使用hexo搭建个人博客

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

    喜欢(594) 浏览(12777)
  5. Qt环境搭建

    喜欢(517) 浏览(23125)

最新评论

  1. 网络编程学习方法和图书推荐 Corleone:啥程度可以找工作
  2. 利用栅栏实现同步 Dzher:作者你好!我觉得 std::thread a(write_x); std::thread b(write_y); std::thread c(read_x_then_y); std::thread d(read_y_then_x); 这个例子中的assert fail并不会发生,原子变量设定了非relaxed内存序后一个线程的原子变量被写入,那么之后的读取一定会被同步的,c和d线程中只可能同时发生一个z++未执行的情况,最终z不是1就是2了,我测试了很多次都没有assert,请问我这个观点有什么错误,谢谢!
  3. boost::asio之socket的创建和连接 项空月:发现一些错别字 :每隔vector存储  是不是是每个. asio::mutable_buffers_1 o或者    是不是多打了个o
  4. 解决博客回复区被脚本注入的问题 secondtonone1:走到现在我忽然明白一个道理,无论工作也好生活也罢,最重要的是开心,即使一份安稳的工作不能给我带来事业上的积累也要合理的舍弃,所以我还是想去做喜欢的方向。
  5. C++ 并发三剑客future, promise和async Yunfei:大佬您好,如果这个线程池中加入的异步任务的形参如果有右值引用,这个commit中的返回类型推导和bind绑定就会出现问题,请问实际工程中,是不是不会用到这种任务,如果用到了,应该怎么解决?
  6. 堆排序 secondtonone1:堆排序非常实用,定时器就是这个原理制作的。
  7. 答疑汇总(thread,async源码分析) Yagus:如果引用计数为0,则会执行 future 的析构进而等待任务执行完成,那么看到的输出将是 这边应该不对吧,std::future析构只在这三种情况都满足的时候才回block: 1.共享状态是std::async 创造的(类型是_Task_async_state) 2.共享状态没有ready 3.这个future是共享状态的最后一个引用 这边共享状态类型是“_Package_state”,引用计数即使为0也不应该block啊
  8. 面试题汇总(一) secondtonone1:看到网络上经常提问的go的问题,做了一下汇总,结合自己的经验给出的答案,如有纰漏,望指正批评。
  9. protobuf配置和使用 熊二:你可以把dll放到系统目录,也可以配置环境变量,还能把dll丢到lib里
  10. 聊天项目(15) 客户端实现TCP管理者 lkx:已经在&QTcpSocket::readyRead 回调函数中做了处理了的。
  11. string类 WangQi888888:确实错了,应该是!isspace(sind[index]). 否则不进入循环,还是原来的字符串“some string”
  12. visual studio配置boost库 一giao里我离giaogiao:请问是修改成这样吗:.\b2.exe toolset=MinGW
  13. 处理网络粘包问题 zyouth: //消息的长度小于头部规定的长度,说明数据未收全,则先将部分消息放到接收节点里 if (bytes_transferred < data_len) { memcpy(_recv_msg_node->_data + _recv_msg_node->_cur_len, _data + copy_len, bytes_transferred); _recv_msg_node->_cur_len += bytes_transferred; ::memset(_data, 0, MAX_LENGTH); _socket.async_read_some(boost::asio::buffer(_data, MAX_LENGTH), std::bind(&CSession::HandleRead, this, std::placeholders::_1, std::placeholders::_2, shared_self)); //头部处理完成 _b_head_parse = true; return; } 把_b_head_parse = true;放在_socket.async_read_some前面是不是更好
  14. 类和对象 陈宇航:支持!!!!
  15. 聊天项目(9) redis服务搭建 pro_lin:redis线程池的析构函数,除了pop出队列,还要free掉redis连接把
  16. 聊天项目(7) visualstudio配置grpc diablorrr:cmake文件得改一下 find_package(Boost REQUIRED COMPONENTS system filesystem),要加上filesystem。在target_link_libraries中也同样加上
  17. interface应用 secondtonone1:interface是万能类型,但是使用时要转换为实际类型来使用。interface丰富了go的多态特性,也降低了传统面向对象语言的耦合性。
  18. 无锁并发队列 TenThousandOne:_head  和 _tail  替换为原子变量。那里pop的逻辑,val = _data[h] 可以移到循环外面吗
  19. 创建项目和编译 secondtonone1:谢谢支持
  20. Qt 对话框 Spade2077:QDialog w(); //这里是不是不需要带括号
  21. 再谈单例模式 secondtonone1:是的,C++11以后返回局部static变量对象能保证线程安全了。
  22. 构造函数 secondtonone1:构造函数是类的基础知识,要着重掌握
  23. Qt MVC结构之QItemDelegate介绍 胡歌-此生不换:gpt, google

个人公众号

个人微信