windows下使用Qt的MinGW8.1.0编译grpc

参考连接:https://blog.csdn.net/u014340533/article/details/125528855

1、编译环境

操作系统:windows10

Qt版本:5.15.2

编译器:MinGW8.1.0

CMake:3.23.1

Git:2.39.2

NASM:2.14.02

配置Qt环境变量

2、grpc源码下载

grpc源码地址:https://github.com/grpc/grpc

因为我的Qt版本是5.15.2,只能用低版本的grpc,目前测试成功的版本是grpc3.16.3

下载指定版本grpc并将子模块一起下载

创建一个文件夹,打开git bash,运行以下命令,下载grpc源码

git clone --recurse-submodules -b v1.36.3 https://github.com/grpc/grpc.git

3、cmake构建 makefile文件

在grpc目录下新建mingw64文件夹,打开CMake,选择源码路径和构建路径

点击Configure按钮,在弹出框中选择MinGW Makefiles,点击Finish按钮。

配置完成,显示Configuring done

点击Generate按钮,生成Makefile文件,显示Generating done

4、minGW 8.1.0编译源码

点击开始,找到Qt目录,找到Qt5.15.2(MinGW 8.10 64-bit),右键选择以管理员身份运行(安装时需要管理员身份)

进入mingw64目录,执行mingw32-make

编译完成

5.安装

编译完成后,执行命令

mingw32-make install

至此,windows下使用MinGW编译grpc完成。

6、在Qt中使用grpc

6.1、配置环境变量

grpc编译完后,在C:\Program Files (x86)\grpc目录下有编译好的grpc的应用程序、库文件、头文件。复制地址C:\Program Files (x86)\grpc\bin,将此地址添加到环境变量

打开cmd,输入protoc,回车,出现图中打印信息,表示配置成功

6.2、写.proto文件,生成cpp文件

新建文件helloworld.proto,输入如下内容

syntax = "proto3";

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;
}

打开cmd,进入helloworld.proto文件所在目录,执行如下命令

protoc.exe ./helloworld.proto --cpp_out=./
protoc.exe ./helloworld.proto --plugin=protoc-gen-grpc="C:/Program Files (x86)/grpc/bin/grpc_cpp_plugin.exe" --grpc_out=./

生成如下四个pb文件

helloworld.pb.h
helloworld.pb.cc
helloworld.grpc.pb.h
helloworld.grpc.pb.cc

6.3、客户端程序

在Qt中新建控制台程序GRpc_HelloWorld_Client,将pb文件添加到GRpc_HelloWorld_Client程序目录下

main.cpp

#include <QCoreApplication>

#include <iostream>
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#include "helloworld.grpc.pb.h"

using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using helloworld::Greeter;
using helloworld::HelloReply;
using helloworld::HelloRequest;

class GreeterClient {
 public:
  GreeterClient(std::shared_ptr<Channel> channel)
      : stub_(Greeter::NewStub(channel)) {}

  // Assembles the client's payload, sends it and presents the response back
  // from the server.
  std::string SayHello(const std::string& user) {
    // Data we are sending to the server.
    HelloRequest request;
    request.set_name(user);

    // Container for the data we expect from the server.
    HelloReply reply;

    // Context for the client. It could be used to convey extra information to
    // the server and/or tweak certain RPC behaviors.
    ClientContext context;

    // The actual RPC.
    Status status = stub_->SayHello(&context, request, &reply);

    // Act upon its status.
    if (status.ok()) {
      return reply.message();
    } else {
      std::cout << status.error_code() << ": " << status.error_message()
                << std::endl;
      return "RPC failed";
    }
  }

 private:
  std::unique_ptr<Greeter::Stub> stub_;
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    GreeterClient greeter(
        grpc::CreateChannel("localhost:50051", grpc::InsecureChannelCredentials()));
    std::string user("world");
    std::string reply = greeter.SayHello(user);
    std::cout << "Greeter received: " << reply << std::endl;
    return a.exec();
}
6.4 服务端程序

在Qt中新建控制台程序GRpc_HelloWorld_Server,将pb文件添加到GRpc_HelloWorld_Server程序目录下

main.cpp

#include <QCoreApplication>
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/ext/proto_server_reflection_plugin.h>
#include <grpcpp/grpcpp.h>
#include <grpcpp/health_check_service_interface.h>
#include "helloworld.grpc.pb.h"

using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using helloworld::Greeter;
using helloworld::HelloReply;
using helloworld::HelloRequest;

// Logic and data behind the server's behavior.
class GreeterServiceImpl final : public Greeter::Service {
  Status SayHello(ServerContext* context, const HelloRequest* request,
                  HelloReply* reply) override {
    std::string prefix("Hello ");
    reply->set_message(prefix + request->name());
    return Status::OK;
  }
};

void RunServer() {
  std::string server_address("0.0.0.0:50051");
  GreeterServiceImpl service;

  grpc::EnableDefaultHealthCheckService(true);
  grpc::reflection::InitProtoReflectionServerBuilderPlugin();
  ServerBuilder builder;
  // Listen on the given address without any authentication mechanism.
  builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  // Register "service" as the instance through which we'll communicate with
  // clients. In this case it corresponds to an *synchronous* service.
  builder.RegisterService(&service);
  // Finally assemble the server.
  std::unique_ptr<Server> server(builder.BuildAndStart());
  std::cout << "Server listening on " << server_address << std::endl;

  // Wait for the server to shutdown. Note that some other thread must be
  // responsible for shutting down the server for this call to ever return.
  server->Wait();
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    RunServer();
    return a.exec();
}
6.5 pro文件配置

GRpc_HelloWorld_Client.pro,GRpc_HelloWorld_Server.pro文件中添加如下内容

GRPC_HOME="C:/Program Files (x86)/grpc"

INCLUDEPATH += $$GRPC_HOME/include

LIBS += -L$$GRPC_HOME/lib -lgrpc++ -lgrpc -lgpr -lgrpc_plugin_support -lgrpc_unsecure -lgrpc++_alts \
-lgrpc++_error_details -lgrpc++_reflection -lgrpc++_unsecure -lgrpcpp_channelz -laddress_sorting \
-lcares -labsl_bad_optional_access -labsl_bad_variant_access -labsl_civil_time \
-labsl_cord \
-labsl_exponential_biased \
-labsl_synchronization -labsl_graphcycles_internal -labsl_hash \
-labsl_city -labsl_hashtablez_sampler -labsl_log_severity \
-labsl_raw_hash_set \
-labsl_spinlock_wait -labsl_status -labsl_statusor -labsl_str_format_internal -labsl_symbolize \
-labsl_stacktrace \
-labsl_debugging_internal -labsl_demangle_internal -labsl_strings -labsl_strings_internal -labsl_malloc_internal \
-labsl_raw_logging_internal -labsl_throw_delegate -labsl_time -labsl_time_zone -labsl_int128 -labsl_base -lre2 \
-lupb -lprotobuf -llibprotoc -lzlib.dll -lssl -lcrypto -ldbghelp

LIBS += -lWS2_32
LIBS += -lAdvapi32 -lbcrypt

DEFINES += _WIN32_WINNT=0x600

注:如果编译出现以下错误,那就将对应的库连接删掉

6.6 编译运行

分别编译GRpc_HelloWorld_Client和GRpc_HelloWorld_Server。编译完成后,需要将C:\Program Files (x86)\grpc\bin目录下的libzlib.dll库copy到GRpc_HelloWorld_Client.exe和GRpc_HelloWorld_Server.exe所在目录下。
运行GRpc_HelloWorld_Server.exe

运行GRpc_HelloWorld_Client.exe

至此Windows下Qt使用grpc介绍完毕。

  • 14
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值