gRPC官方Windows安装说明:
- Install Visual Studio 2015 or 2017 (Visual C++ compiler will be used).
- Install Git.
- Install CMake.
- Install Active State Perl (
choco install activeperl
) - required by boringssl - Install Go (
choco install golang
) - required by boringssl - Install yasm and add it to
PATH
(choco install yasm
) - required by boringssl - (Optional) Install Ninja (
choco install ninja
)
在Windows上源码编译安装gRPC还是很麻烦的需要MSVC编译器、git、cmake、Perl、go、yasm、Ninja等工具,一些工具的安装可以使用choco(Windows上的包管理工具)来安装。
准备环境
https://git-scm.com/上下载git,当前版本2.23.0。
https://cmake.org/download/上下载cmake,下载cmake-3.15.3-win64-x64.zip,解压后方式何时位置,将cmake工具所在bin目录加入到系统环境变量Path。
安装Perl、go、yasm等
使用choco安装activeperl在我的电脑上很慢,慢的很,直接在官网下载又需要注册登陆用户,也很麻烦,最后直接在第三方网站下载,直接来安装!
下载gRPC源码
新建一个文件夹,命名为grpc_plugs,右击空白处选择Git Bash Here
git clone https://gitee.com/githubplus/grpc.git
cd grpc
git tag
git checkout v1.20.0(一定要切换到这个版本,不然grpc 依赖的第三方库不一样)
查看当前版本
git branch -v
下载依赖
修改.gitmodules
文件,替换其中的github源为gitee源
[submodule "third_party/zlib"]
path = third_party/zlib
url = https://gitee.com/githubplus/zlib
# 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/githubplus/protobuf.git
branch = 3.0.x
[submodule "third_party/gflags"]
path = third_party/gflags
url = https://gitee.com/githubplus/gflags.git
[submodule "third_party/googletest"]
path = third_party/googletest
url = https://gitee.com/githubplus/googletest.git
[submodule "third_party/boringssl"]
path = third_party/boringssl
url = https://gitee.com/githubplus/boringssl.git
[submodule "third_party/benchmark"]
path = third_party/benchmark
url = https://gitee.com/githubplus/benchmark.git
[submodule "third_party/boringssl-with-bazel"]
path = third_party/boringssl-with-bazel
url = https://gitee.com/githubplus/boringssl.git
[submodule "third_party/cares/cares"]
path = third_party/cares/cares
url = https://gitee.com/githubplus/c-ares.git
branch = cares-1_12_0
[submodule "third_party/bloaty"]
path = third_party/bloaty
url = https://gitee.com/githubplus/bloaty.git
[submodule "third_party/abseil-cpp"]
path = third_party/abseil-cpp
url = https://gitee.com/githubplus/abseil-cpp
[submodule "third_party/libcxxabi"]
path = third_party/libcxxabi
url = https://gitee.com/githubplus/libcxxabi.git
branch = release_60
[submodule "third_party/libcxx"]
path = third_party/libcxx
url = https://gitee.com/githubplus/libcxx.git
branch = release_60
[submodule "third_party/data-plane-api"]
path = third_party/data-plane-api
url = https://gitee.com/githubplus/data-plane-api.git
[submodule "third_party/googleapis"]
path = third_party/googleapis
url = https://gitee.com/githubplus/googleapis.git
[submodule "third_party/protoc-gen-validate"]
path = third_party/protoc-gen-validate
url = https://gitee.com/githubplus/protoc-gen-validate.git
[submodule "third_party/upb"]
path = third_party/upb
url = https://gitee.com/githubplus/upb.git
然后下载第三方依赖
git submodule update --init
进入到third_party/protobut 中,更改.gitmodules
类似修改protobuf
的.gitmodules
后,执行git submodule update --init
[submodule "third_party/benchmark"]
path = third_party/benchmark
url = https://gitee.com/githubplus/benchmark.git
[submodule "third_party/googletest"]
path = third_party/googletest
url = https://gitee.com/githubplus/googletest.git
ignore = dirty
下载完毕后,找到并打开文件./grpc/third_party/zlib/gzguts.h找到
#ifdef _WIN32
#inlcude <stddef.h>
#endif
改为
#ifdef _WIN32
#include <stddef.h>
#pragma warning(disable:4996)
#endif
屏蔽一些警告,感觉没啥实际作用,只是照着做而已。
VS2015 编译gRPC源码
mkdir .build
cd .build
cmake –version
cmake .. -G "Visual Studio 14 2015 Win64" -DCMAKE_BUILD_TYPE=Release
配置makefile,过程比较长,期间没有出现error啥的就没啥问题,会生成grpc.sln。
cmake --build .
上述命令执行VS2017编译操作,和直接打开grpc.sln执行编译一样的效果。
耐心等待编译完成…
编译完成后生成gRPC以及第三方依赖库的lib静态库文件,见下图所示:
HelloWorld
gRPC自带的C++ HelloWorld 例子位于.\grpc\examples\cpp\helloworld
在VS2017中新建空的C++工程gRPCTest
将helloworld.proto拷贝到工程目录下
将生成的protoc.exe拷贝到工程目录下
工程目录下运行cmd执行protoc编译命令生成生成源代码文件helloworld.pb.h,helloworld.pb.cpp,helloworld.grpc.pb.h,helloworld.grpc.pb.cpp。
protoc -I="./protos" --grpc_out="./protos" --plugin=protoc-gen-grpc=“E:\LearningOpensourceLib\gRPC\grpc_plugs\grpc\.build\Debug\grpc_cpp_plugin.exe " "./protos\helloworld.proto"
protoc -I="./protos" --cpp_out="./protos" "./protos\helloworld.proto"
拷贝grpc_cpp_plugin.exe,greeter_client.cc,greeter_server.cc至工程目录下
将文件都添加至工程中去,最终的工程结构如下:
选择Debug – x64构建配置
配置include目录以及lib目录和lib导入:我这里直接添加的绝对路径
需要配置预处理器定义_WIN32_WINNT=0x0A00,否则会报“Please compile grpc with _WIN32_WINNT of at least 0x600 (aka Windows Vista)”的错误。
客户端代码新增std::cin.get()避免窗口一闪而退:
服务端代码:
编译服务端时,请屏蔽掉客户端的main函数代码
编译客户端时,请屏蔽掉服务端的main函数代码
生成exe文件改名,并将zlibd.dll放在一起
分别运行grpc-server.exe和grpc-client.exe