gRPC編譯教程


  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大於等於0x600 ,添加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編譯

測試結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM