当前位置: 移动技术网 > IT编程>开发语言>C/C++ > gRPC编译教程

gRPC编译教程

2019年05月09日  | 移动技术网IT编程  | 我要评论

黄鳝门种子,追梦3dna,博瑞客

  windows平台的编译

一.编译openssl

  ① 安装perl(可以使用activeperl),执行perl configure vc-win64a no-asm 。在这里解释一下参数含义,vc-win64a是指amd64也就是我们常用的x64,还要一个vc-win64i是安腾itanium,目前没有使用,no-asm是不使用汇编。

  ② 执行ms\do_win64a.bat

  ③ 使用vs的x86_x64命令行工具执行nmake -f ms\ntdll.mak 生成动态库。

  ④ 使用vs的x86_x64命令行工具执行nmake -f ms\nt.mak 生成静态库,这步可以不用执行一般生成动态库即可。

  备注:遇到过的常见问题,比如ml64、rc等命令不存在以及x86和x64,这都是没有使用vs的x84_x64命令行工具导致的。

二.编译zlib

  zlib的编译非常简单,在contrib\vstudio选择任意一个文件夹然后生成全部即可。

  备注:最好生成release x64的,releasewithoutasm可能会有某些project生成失败,这是因为它们的lib使用的是release的。

三.编译grpc

  ① 将下载的好的cares、protobuf、gflags、benchmark源码放到third_party文件夹下的对应文件夹中(cares需要放到cares\cares中)。

  ② 使用cmake打开,并将grpc_zlib_provider、grpc_ssl_provider由module改成package

  ③ 在cmake中配置好zlib_root、zlib_binaray、lib_eay_debug、lib_eay_release、ssl_eay_debug、ssl_eay_release、oepnssl_include_dir这些变量

  ④ configure,像zlib_root cmake is ignoring the variable、 grpc_install will be forced to false等警告可以忽略

  ⑤ generate,启动vs全部生成

测试案例:

  helloserivce.proto

 1 syntax = "proto3";
 2 
 3 service helloservice{
 4     rpc sayhello(request) returns(response){}
 5 }
 6 
 7 message request{
 8     int32 id = 1;
 9     string req = 2;
10     bytes data = 3;
11 }
12 
13 message response{
14     int32 id = 1;
15     string resp = 2;
16     bytes data = 3;
17 }

  执行protoc.exe helloservice.proto -i=. --cpp_out=. protoc.exe helloservice.proto -i=. --grpc_out=. --plugin=protoc-gen-grpc=grpc_cpp_plugin.exe生成编译产物

  服务器:helloservice_server.cpp

 1 #include "helloservice.grpc.pb.h"
 2 
 3 #include <iostream>
 4 
 5 
 6 #include <grpc/grpc.h>
 7 #include <grpcpp/server.h>
 8 #include <grpcpp/server_builder.h>
 9 #include <grpcpp/server_context.h>
10 #include <grpcpp/security/server_credentials.h>
11 
12 
13 class helloserviceimpl final :public helloservice::service 
14 {
15     grpc::status sayhello(grpc::servercontext* context, const ::request* request, ::response* response)
16     {
17         std::cout << request->data() << std::endl;
18 
19         response->set_data("hello from server");
20 
21         return grpc::status::ok;
22     }
23 };
24 
25 
26 void runserver() 
27 {
28     std::string server_address("127.0.0.1:57501");
29     helloserviceimpl service;
30 
31     grpc::serverbuilder builder;
32     builder.addlisteningport(server_address, grpc::insecureservercredentials());
33     builder.registerservice(&service);
34     std::unique_ptr<grpc::server> server(builder.buildandstart());
35     std::cout << "server listening on " << server_address << std::endl;
36     server->wait();
37 }
38 
39 int main(int argc, char ** argv)
40 {
41     runserver();
42     return 0;
43 }

  客户端:helloservice_client.cpp

 1 #include "helloservice.grpc.pb.h"
 2 
 3 #include <iostream>
 4 #include <memory>
 5 
 6 #include <grpc/grpc.h>
 7 #include <grpcpp/channel.h>
 8 #include <grpcpp/client_context.h>
 9 #include <grpcpp/create_channel.h>
10 #include <grpcpp/security/credentials.h>
11 
12 class helloserviceclient
13 {
14 public:
15     helloserviceclient(std::shared_ptr<grpc::channelinterface> channel) :stub(helloservice::newstub(channel))
16     {
17 
18     }
19     bool sayhello()
20     {
21         grpc::clientcontext context;
22         request req;
23         req.set_data("send from client");
24         response resp;
25         stub->sayhello(&context,req,&resp);
26         std::cout << resp.data() << std::endl;
27         return true;
28     }
29 private:
30     std::unique_ptr<helloservice::stub> stub;
31 };
32 
33 
34 
35 
36 int main(int argc,char ** argv)
37 {
38     helloserviceclient client(grpc::createchannel("127.0.0.1:57501", grpc::insecurechannelcredentials()));
39 
40     client.sayhello();
41 
42     return 0;
43 }

编译时需要注意设置_win32_winnt大于等于0x601 ,添加address_sorting.lib、gpr.lib、grpc.lib、grpc++.lib、libprotobuf.lib、zlibstat.lib、libeay32.lib、ssleay32.lib、ws2_32.lib、cares.lib这些lib。

测试结果:

服务器:

客户端:

 

  fedora编译教程

  直接执行sudo dnf install grpc grpc-devel protobuf-compiler grpc-plugins即可

测试案例:

  代码同上

  备注:执行protoc helloservice.proto -i=. --grpc_out=. --plugin=protoc-gen-grpc=grpc_cpp_plugin可能会出现下面的错误:

    grpc_cpp_plugin: program not found or is not executable

    --grpc_out: protoc-gen-grpc: plugin failed with status code 1.

  改成执行protoc helloservice.proto -i=. --grpc_out=. --plugin=protoc-gen-grpc=/usr/bin/grpc_cpp_plugin就行了

  使用上面的代码执行g++ -o client helloservice_client.cpp helloservice.grpc.pb.cc helloservice.pb.cc -lgrpc++ -lprotobuf  -std=c++17和 g++ -o server helloservice_server.cpp helloservice.grpc.pb.cc helloservice.pb.cc -lgrpc++ -lprotobuf -std=c++17编译

测试结果:

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网