gRPC是開源架構,項目代碼在github上,是以首先要安裝github。
雖然在github的gRPC首頁上提供了源代碼打包下載下傳,但是gRPC的依賴元件就無法自動擷取了。
(第三方庫目錄在third_party下面)
用vs2015打開
vsprojects/grpc.sln
1,删除工程boringssl
2,在\grpc\third_party\zlib\gzguts.h
中将
#ifdef _WIN32
# include <stddef.h>
#endif
改成
#pragma warning(disable:4996)
去掉警告。
完成:可以編譯
說明:NuGet有還原庫,需要等待完成
gRPC依賴protobuffer進行消息編碼,是以需要依賴protobuffer。(詳細見:grpc\third_party\protobuf\cmake\README.md)
需要git,cmake支援
cmd打開vs指令行工具(Windows Desktop Command Prompts/VS2015 x64 x86 相容工具指令提示符)
cd 到grpc目錄
cd protobuf
git clone -b release-1.7.0 https://github.com/google/googlemock.git gmock
cd gmock
git clone -b release-1.7.0 https://github.com/google/googletest.git gtest
cd ..\cmake
mkdir build & cd build
mkdir release & cd release
cmake -G "NMake Makefiles" ^
-DCMAKE_BUILD_TYPE=Release ^
-DCMAKE_INSTALL_PREFIX=../../../../install ^
../..
cd ..
mkdir debug & cd debug
-DCMAKE_BUILD_TYPE=Debug ^
mkdir solution & cd solution
cmake -G "Visual Studio 14 2015 Win64" ^
打開grpc\third_party\protobuf\cmake\build\solution下protobuf.sln
編譯成功
生成檔案:
protoc.exe
libprotobuf.lib
libprotoc.lib
後續有用到
将編譯好的Debug,Release檔案夾拷貝到grpc\third_party\protobuf\cmake\目錄下(Debug下面的lib檔案後邊的d需要去掉)
打開grpc\vsprojects\grpc_protoc_plugins.sln編譯生成可執行檔案
完成
生成檔案:
grpc_cpp_plugin.exe
grpc_csharp_plugin.exe
grpc_node_plugin.exe
grpc_objective_c_plugin.exe
grpc_python_plugin.exe
grpc_ruby_plugin.exe
(詳細見:grpc\examples\protos\helloworld.proto)
将proto.exe、helloworld.proto、grpc_cpp_plugin.exe拷貝到一個檔案夾中,grpc_cpp_plugin.exe是gRPC的protoc插件,生成方法參考上文。
建立一個bat檔案,包含以下指令:
生成了兩套檔案
其中前兩個是protoc生成的,後兩個是插件生成的。
這些包括:
所有的填充,序列化和擷取我們請求和響應消息類型的 protocol buffer 代碼
名為 Greeter的類,包含
為了用戶端去調用定義在 Greeter服務的遠端接口類型(或者 存根 )
讓伺服器去實作的兩個抽象接口,同時包括定義在 Greeter中的方法。
詳細見:https://doc.oschina.net/grpc?t=57966
中
依照grpc\examples\cpp\helloworld下client,server例子
greeter_async_client.cc
greeter_async_client2.cc
greeter_async_server.cc
greeter_client.cc
greeter_server.cc
将剛剛生成的檔案放入工程源代碼目錄$(SolutionDir)并包含進工程
GrpcTest\GrpcTest\src\protobuf
将:grpc\include,grpc\third_party\protobuf\src中的檔案拷貝到include目錄(自己的庫目錄)
三個頭檔案目錄,一個是剛生成的的通路類路徑,一個是gRPC的頭檔案,一個是protobuf的頭檔案.
将:
grpc\third_party\protobuf\cmake\Release;
grpc\vsprojects\Release;
grpc\third_party\zlib\solution\Release;
grpc\vsprojects\packages\grpc.dependencies.openssl.1.0.204.1\build\native\lib\v120\Win32\Release\static
放入lib目錄(自己的lib庫目錄)
四個庫檔案路徑:protobuf庫路徑、grpc庫路徑、zlib庫路徑、openssl庫路徑。
沒有使用boringssl,openssl的庫是從NuGet下載下傳的package中找到的。
庫檔案
将gRPC的C++ example的代碼拷貝到我們剛建立的項目中,編譯,出現一些error:
解決:在項目屬性中的<code>Preprocessor Definitions</code>中添加<code>_WIN32_WINNT=0x600</code>
解決:在項目屬性中的Preprocessor Definitions中添加
解決:隻需要主程式和靜态庫都采用同一種Runtime Libray編譯即可。
在項目屬性C/C++中的 代碼生成 的 運作庫 選擇 Multi-threaded(/MT)
OpenSSl我是添加的NuGet庫來解決的
點選項目右鍵=>管理NuGet程式包
點選浏覽=>輸入grpc.dependencies.openssl
找到grpc.dependencies.openssl =>點選安裝(我是用的v1.0.204.1版本)
完成,
編輯通過
依照
c++生成helloworld伺服器程式
流程,
我使用的
兩個測試來做的測試
說明:
<a href="http://download.csdn.net/detail/xie1xiao1jun/9630779" target="_blank">點選打開連結</a>