window gRPC in c++

项目地址:https://gitee.com/c15721527020/gRPCDemo/tree/master/HelloWorld

grpc编译生成文件下载地址:https://gitee.com/c15721527020/gRPCDemo/blob/master/ccinstall.zip

1:grpc编译阶段

1:安装 git cmake nasm
2:git clone https://github.com/grpc/grpc
3:cd grpc
4:git submodule update --init
这里有可能会出现问题,因为qiang 的原因,所以如果 third_party 安装失败,可以根据报错提示,一个一个手动安装,手动安装的过程

1:cd third_party
2:git clone git.xxxxxxxx.git(这里根据报错提示安装,把https 切换成 http)

5:mkdir .build
6:cd .build 7:cmake .. -G "Visual Studio 16 2019" -DCMAKE_INSTALL_PREFIX=../ccinstall 这里可以使用-DCMAKE_INSTALL_PREFIX指定安装目录
8:然后进入 .build目录 打开 sln文件 然后 在visual studio 2019 中 点击全部生成,然后再运行INSTALL目录执行安装
9:最终的结果 如图

2:生成 proto

编译完成会生成install文件夹,后面所有用到的头文件和lib文件都在 这个文件夹下面

文件夹层级结构如图:

进入 bin文件夹,打开cmd命令行,执行如下操作:
protoc --grpc_out=. --plugin=protoc-gen-grpc=grpc_cpp_plugin.exe helloworld.proto
protoc --cpp_out=. helloworld.proto
生成目录如下:

helloworld.proto文件如下

syntax = "proto3";

option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";

package helloworld;

// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
string name = 1;
}

// The response message containing the greetings
message HelloReply {
string message = 1;
}

3:项目创建

右键项目属性



获取lib文件夹下所有lib文件:
dir *.lib /s /b > lists.txt
复制第一步骤生成的
helloworld.grpc.pb.h helloworld.pb.h helloworld.grpc.pb.cc helloworld.pb.cc 文件到新创建的项目目录,如图

创建greeter_client.cc, greeter_server.cc这两个文件可以在grpc github上找到 路径如下

然后将文件全部包含在项目中
分别注释 greeter_client.cc 的main函数和 greeter_server.cc 的main函数进行编译即可

运行结果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章