Windows+VS2015使用gRPC

 

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。

  安装choco

  

  安装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

  

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Windows使用 Qt 和 gRPC 进行开发,你需要进行以下步骤: 1. 安装 gRPC:你可以从 gRPC 的 GitHub 仓库下载预编译的二进制文件,或者使用 vcpkg 包管理器进行安装。具体的安装指南可以参考 gRPC 的官方文档。 2. 创建一个新的 Qt 项目:打开 Qt Creator,选择 "New Project",选择 "Qt Widgets Application" 或者其他适合的项目模板。 3. 配置项目文件:在项目文件(.pro)中添加 gRPC 相关的配置。你需要添加以下内容: ``` QT += network # 添加 gRPC 相关的模块 CONFIG += link_pkgconfig PKGCONFIG += grpc++ grpc # 添加 gRPC 的头文件路径 INCLUDEPATH += /path/to/grpc/include # 添加 gRPC 的库文件路径 LIBS += -L/path/to/grpc/libs -lgrpc++ -lgrpc ``` 请确保将上述路径替换为你实际安装 gRPC 的路径。 4. 编写 gRPC 服务和客户端代码:在项目中创建一个新的源文件,编写 gRPC 的服务定义和客户端代码。你可以使用 Protocol Buffers(protobuf)来定义 gRPC 服务接口,并使用 protoc 工具生成相应的代码。 5. 生成 gRPC 代码:使用 protoc 工具将你的 protobuf 文件转换为 C++ 代码。你可以使用以下命令: ``` protoc -I=path/to/protobuf --grpc_out=. --plugin=protoc-gen-grpc=path/to/grpc/bins/grpc_cpp_plugin your_service.proto ``` 请将上述路径替换为你实际的路径,并将 your_service.proto 替换为你的 protobuf 文件名。 6. 将生成的代码添加到项目中:将生成的 C++ 代码文件添加到你的 Qt 项目中,并在项目文件中将其包含进来。 7. 在 Qt 项目中使用 gRPC:在你的 Qt 项目中,你可以使用生成的 gRPC 代码来创建 gRPC 的服务和客户端对象,并进行相应的调用。 这些步骤应该能帮助你在 Windows使用 Qt 和 gRPC 进行开发。如果你遇到了问题,可以参考 gRPC 的官方文档或者在社区寻求帮助。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值