第一步:新建文件夹,命名为:grpcSetting,进入grpcSetting,创建include和staticlib两个文件夹。打开staticlib文件夹,在其下面创建debug和release
第二步:打开C:\Program Files (x86)\grpc文件夹,进入下面的include文件,复制其下面的所有文件,粘贴到grpcSetting文件夹下的include
第三步:打开上一篇下载的grpc所在的目录,打开D:\grpc-plug\grpc_v1.22.0\grpc\.build\Debug,将其下面的lib复制粘贴到刚刚创建的staticlib文件中的debug中
第四步:创建Qt项目Qt Console Application 命名为grpcDemo,将上面创建好的grpcSetting下的include和release复制到项目grpcDemo所在的目录下面。
第五步:右击项目选择“添加库”,选择外部库,点击下一步------>平台勾选Windows,链接选择静态,WIndows选择第一个,库文件选择刚刚放置的staticli里debug下的静态库。重复第五步,知道所有的静态库全部加入。
第六步:打开grpcSetting文件夹,在下面创建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 = "LWH";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
rpc SayBey (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
bytes data = 2;
int32 id = 3;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
第七步:打开grpc所在的目录,搜索grpc_cpp_plugin.exe,将其复制粘贴到grpcSetting,将protoc.exe也复制粘贴到下面,运行cmd,定位到grpcSetting中,分别执行下面两条代码 ,会生成下面四个(值得注意的是,protoc.exe要与编译器中的protoc版本一致,否则会出现头文件未定义的现象)
protoc.exe -I=. --grpc_out=. --plugin=protoc-gen-grpc=./grpc_cpp_plugin.exe helloworld.proto
protoc.exe -I=. --cpp_out=. helloworld.proto
第八步:将其四个复制粘贴到Qt项目下,添加到项目grpcDemo中。
第九步:打开main.c,复制下面代码:
#include "helloworld.grpc.pb.h"
#include
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using helloworld::HelloRequest;
using helloworld::HelloReply;
using helloworld::Greeter;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::shared_ptr channel = grpc::CreateChannel("127.0.0.1:5678", grpc::InsecureChannelCredentials());
std::unique_ptr stub = helloworld::Greeter::NewStub(channel);
while (true) {
::grpc::ClientContext context;
::helloworld::HelloRequest request;
::helloworld::HelloReply response;
request.set_name("aljdflkjasd;lfj");
stub->SayHello(&context, request, &response);
std::cout << response.Utf8DebugString();
std::string ch;
std::cin >> ch;
}
return a.exec();
}
运行起来,此时Qt配置完成。