Qt使用GRPC之grpc配置

本文详细介绍了如何在Qt环境中配置并使用gRPC,包括创建必要的文件夹结构、复制gRPC库文件、设置Qt项目、编写proto文件、生成代码、以及在Qt项目中调用gRPC服务的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步:新建文件夹,命名为: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 <grpcpp/grpcpp.h>

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> channel = grpc::CreateChannel("127.0.0.1:5678", grpc::InsecureChannelCredentials());
    std::unique_ptr<helloworld::Greeter::Stub> 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配置完成。

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林零七

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值