gRPC的C 編譯及簡單使用

2021-10-25 02:16:18 字數 4607 閱讀 1427

在git轉殖grpc專案後,在該目錄下執行–recursive或update子模組submodules命令;如果想要編譯動態庫.so檔案,執行cmake時新增-dbuild_shared_libs=on.

$ mkdir -p cmake/build

$ cd cmake/build

$ cmake -dgrpc_install=on \

-dgrpc_build_tests=off \

-dcmake_install_prefix=

$my_install_dir \

../..

# cmake ../.. -dcmake_install_prefix=/usr

$ make

$ make

install

對於解決依賴項,grpc的cmake構建系統有兩個選擇。cmake可以為你編譯構建依賴庫,或者尋找已安裝到作業系統的庫檔案並使用他們構建grpc。執行方式通過cmake中 grpc__provider 等引數的控制,例如grpc_cares_provider。這些選項如下執行:

例如,你可以設定grpc_cares_provider=module, 這樣cmake將在構建grpc之前構建c-ares。另一方面,如果設定grpc_cares_provider=package, cmake將會尋找已經安裝到系統的c-ares並複製它來構建grpc。

如下步驟通過cmake安裝grpc:

如果cmake是v3.13或更高,需要通過"module"模式來構建grpc的第三方依賴庫並且一鍵安裝。而如果構建小於1.27版本的grpc或使用小於3.13的cmake,需要選擇"package"而不是"module"模式來構建第三方依賴。這就需要你確保當前系統的第三方依賴庫可用。如下示例表示在安裝grpc之前,如何通過cmake安裝依賴庫。

# 所有grpc的第三方依賴庫需要已安裝成功

$ cmake ../.. -dgrpc_install=on \

-dcmake_build_type=release \

-dgrpc_absl_provider=package \

-dgrpc_cares_provider=package \

-dgrpc_protobuf_provider=package \

-dgrpc_re2_provider=package \

-dgrpc_ssl_provider=package \

-dgrpc_zlib_provider=package

$ make

$ make

install

交叉編譯示例

編譯範例helloworld

# 進入grpc專案中

cd examples/cpp/helloworld

# 準備構建,這裡一定確保cmake版本正確

$ mkdir -p cmake/build

$ pushd cmake/build

$ cmake ../..

$ make -j

執行服務端

$ ./greeter_server
不同的終端執行客戶端程式,顯示如下說明已執行成功

$ ./greeter_client

greeter received: hello world

//14: failed to connect to all addresses

// greeter received: rpc failed

//如果出現該問題,說明沒有連線成功;檢視是否服務端開啟,且埠號和客戶端一致;檢視是否依賴項問題;

公升級grpc服務現在看下服務端如何用外部的一些方法更新應用,同時可用於客戶端; grpc服務裡用使用了protocol buffers,我們可以找到很多文章解釋如何在.proto檔案定義服務;現在你需要知道的是,服務端和客戶端都需要sayhello()的rpc函式方式,客戶端通過它傳遞hellorequest型別引數到服務端,服務端返回helloresponse型別引數。通過examples/protos/helloworld.proto路徑檢視如下定義:

// the greeting service definition.

service greeter

}// the request message containing the user's name.

message hellorequest

// the response message containing the greetings

message helloreply

然後新增方法sayhelloagain(), 以及相同的請求和響應型別:

// the greeting service definition.

service greeter

// sends another greeting

rpc sayhelloagain (hellorequest) returns (helloreply)

}// the request message containing the user's name.

message hellorequest

// the response message containing the greetings

message helloreply

重新生成**在使用新服務方法前,需要重新編譯更新下proto檔案。然後從目錄examples/cpp/helloworld/cmake/build下執行

$ make -j
這樣重新生成了helloworld.pb.和helloworld.grpc.pb., 其中包含了服務端和客戶端類檔案, 以及類的傳遞、序列化、以及獲取的請求和響應型別。 實際是執行編譯helloworld.proto檔案為cpp等檔案,類似protoc --cpp_out=. helloworld.proto,並引用其編譯為二進位制檔案;建立cpp的grpc檔案,採用命令protoc --grpc_out . --cpp_out . -i . --plugin=protoc-gen-grpc=/usr/local/bin/grpc_cpp_plugin abc.proto;

公升級並執行程式

儘管上一步新生成了新**,但仍需要在example應用中人工修改**,執行呼叫新方法;

從example根目錄下開啟cpp/helloworld/greeter_server.cc,執行如下方法:

```protobuf

class greeterserviceimpl final : public greeter::service

status sayhelloagain(servercontext* context, const hellorequest* request,helloreply* reply) override

};```

- 客戶端

現在protobuf根庫中新的sayhelloagain()已經可用。我們需要在greeterclient中按照相同方式,完成sayhello()和sayhelloagain()函式,檔案greeter_client.cc中:

```protobuf

class greeterclient

std::string sayhelloagain(const std::string& user) else

}```

- 最終在主函式中執行新方法:

```bash

int main(int argc, char** ar**)

```

執行執行在examples/cpp/helloworld/cmake/build目錄中執行如下命令:

$ make -j

# 執行服務端程式

$ ./greeter_server

# 在不同終端執行如下客戶端程式

$ ./greeter_client

# 看到如下輸出

greeter received: hello world

greeter received: hello again world

如果想要非同步客戶端和服務端,可以在原始碼目錄中看到greeter_async_server,greeter_async_client等**檔案。

讀者可以參看grpc中文手冊中c++教程進行操作。這裡不進一步展開。該程式主要是路線指引,借助json檔案進行計算;

pjsip的編譯及簡單使用

2.編譯 目錄下的readme.txt檔案中有編譯說明,關於windows下的注意點如下 building win32 target with microsoft visual studio 新建乙個空檔案pjlib include pj config site.h後,編譯pjsua工程出現以下錯誤...

pjsip的編譯及簡單使用

2.編譯 目錄下的readme.txt檔案中有編譯說明,關於windows下的注意點如下 building win32 target with microsoft visual studio generally we can just do these steps 1.visual studio 6...

SDL編譯及簡單使用

型別 sdl windwoevent 視窗事件 sdl keyboardevent 鍵盤事件 sdl mousemotionevent 滑鼠事件 事件處理 sdl pollevent 輪訓 sdl waitevent 等待事件,超時機制 推薦使用 包含sdl標頭檔案 include 初始化sdl s...