【C++】gRPC开发环境搭建(Windows & Linux)
由于github链接国内有时访问不了,导致安装grpc及其依赖时会报错,因此采用gitee安装,并更换其依赖为对应的gitee地址。windows选择MinGW进行编译,Linux下使用普通的make即可。
下载源码
这里使用 gRPC 最新版本源代码进行编译和安装,版本号为 v1.50.0, 以下为安装步骤:
一、使用Git克隆gRPC到本地
在终端中打开某一文件夹,建议新建一个单独存放 gRPC 源代码的文件夹,输入:
copygit clone -b v1.50.0 https://github.com/grpc/grpc
如果github访问不了,我将grpc v1.50版本搬运至gitee,同时完成了后续的修改,大家只需clone后跳到编译部分.
copygit clone https://gitee.com/invalid_args/grpc_v1.50.git
安装依赖
gRPC 编译时需要安装一些第三方依赖,使用命令:
copy
git submodule update --init
将会自动安装,但由于国内网络或者其他原因导致下载速度非常慢,所以我们需要手动修改"grpc/.gitmodules"将第依赖安装源更换为国内 gitee 同步仓库,下载效率将会大大改善,"grpc/.gitmodules"文件如下:
copy[submodule "third_party/abseil-cpp"] path = third_party/abseil-cpp url = https://gitee.com/suuair/abseil-cpp.git [submodule "third_party/benchmark"] path = third_party/benchmark url = https://gitee.com/chahan/benchmark.git [submodule "third_party/bloaty"] path = third_party/bloaty url = https://gitee.com/GgBoy_ssz/bloaty.git [submodule "third_party/boringssl-with-bazel"] path = third_party/boringssl-with-bazel url = https://gitee.com/GgBoy_ssz/boringssl.git [submodule "third_party/cares/cares"] path = third_party/cares/cares url = https://gitee.com/RicLee/c-ares.git [submodule "third_party/envoy-api"] path = third_party/envoy-api url = https://gitee.com/RicLee/data-plane-api.git [submodule "third_party/googleapis"] path = third_party/googleapis url = https://gitee.com/GgBoy_ssz/googleapis.git [submodule "third_party/googletest"] path = third_party/googletest url = https://gitee.com/bosspoi/googletest.git [submodule "third_party/libuv"] path = third_party/libuv url = https://gitee.com/RicLee/libuv.git [submodule "third_party/opencensus-proto"] path = third_party/opencensus-proto url = https://gitee.com/RicLee/opencensus-proto.git [submodule "third_party/opentelemetry"] path = third_party/opentelemetry url = https://gitee.com/EBServerStudy/opentelemetry-proto.git [submodule "third_party/protobuf"] path = third_party/protobuf url = https://gitee.com/EBServerStudy/protobuf.git [submodule "third_party/protoc-gen-validate"] path = third_party/protoc-gen-validate url = https://gitee.com/arzhe/protoc-gen-validate.git [submodule "third_party/re2"] path = third_party/re2 url = https://gitee.com/GgBoy_ssz/re2.git [submodule "third_party/xds"] path = third_party/xds url = https://gitee.com/EBServerStudy/xds.git [submodule "third_party/zlib"] path = third_party/zlib url = https://gitee.com/RicLee/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
打开 grpc/.gitmodules 文件,更新为上述内容
执行命令 git submodule sync
同步 URL
copygit submodule sync
执行命令 git submodule update --init
下载第三方依赖
copygit submodule update --init
下载完成后即可开始编译
Windows
环境: MinGW + CMake
如果gcc版本超过11,需要对部分源码进行修改
在
copy#include<limits>
windows下还需要修改<your_grpc_path>\third_party\re2\util\pcre.h中的503行 :
copymutable int32_t hit_limit_; # 修改为: mutable int hit_limit_
然后即可执行以下操作开始编译:
copymkdir build cd build cmake .. -G "MinGW Makefiles" mingw-make.exe -j8
之后以管理员身份打开 cmd 或者 powershell, cd 到grpc/build目录,执行mingw-make install -j8
即可将grpc安装到program files (x64)
目录
copymingw-make install -j8
Linux
Linux下同样需要在
copy#include<limits>
然后开始编译
copymkdir build cd build cmake .. make -j8 sudo make install
验证
官方提供了一套测试样例,位于 grpc/examples/目录下,找到 cpp/helloworld 文件夹,这里存放我们需要测试的程序.
同样cmake + make后,运行server程序和client程序即可.
注: 需要有protobuff环境
大部分内容来自该作者,链接:https://blog.csdn.net/qq_37434641/article/details/127561281
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步