此文檔是windwos grpc c++ 編譯 ,基於 vs2015 編譯完成
獲取gRPC源碼
gRPC是開源框架,項目代碼在github上,所以首先要安裝github。
github安裝后,在指定文件夾中,執行Git命令就可以獲取gRPC的所有源碼。
git clone
https://github.com/grpc/grpc
雖然在github的gRPC主頁上提供了源代碼打包下載,但是gRPC的依賴組件就無法自動獲取了。
獲取gRPC的依賴組件
正如所有的項目一樣,gRPC也是需要依賴第三方庫。由於在gRPC中已經通過git的.gitmodules
文件定義了依賴組件,所以只需執行git命令就可以自動獲取所有的依賴組件。
cd grpc
git submodule update --init
vs工程調試
用vs2015打開
vsprojects/grpc.sln
1,刪除工程boringssl
2,在\grpc\third_party\zlib\gzguts.h
中將
- #ifdef _WIN32
- # include <stddef.h>
- #endif
改成
- #ifdef _WIN32
- # include <stddef.h>
- #pragma warning(disable:4996)
- #endif
去掉警告。
完成:可以編譯
說明:NuGet有還原庫,需要等待完成
編譯protobuffer
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
- cmake -G "NMake Makefiles" ^
- -DCMAKE_BUILD_TYPE=Debug ^
- -DCMAKE_INSTALL_PREFIX=../../../../install ^
- ../..
- cd ..
- mkdir solution & cd solution
- cmake -G "Visual Studio 14 2015 Win64" ^
- -DCMAKE_INSTALL_PREFIX=../../../../install ^
- ../..
編譯成功
后續有用到
將編譯好的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
c++生成helloworld服務器程序
syntax = "proto3";
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";
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;
}
2. 生成訪問代碼
將proto.exe、helloworld.proto、grpc_cpp_plugin.exe拷貝到一個文件夾中,grpc_cpp_plugin.exe是gRPC的protoc插件,生成方法參考上文。
創建一個bat文件,包含以下命令:
protoc.exe -I=. --grpc_out=. --plugin=protoc-gen-grpc=.\grpc_cpp_plugin.exe helloworld.proto
protoc.exe -I=. --cpp_out=. helloworld.proto
生成了兩套文件
hellowworld.pb.h 聲明生成的消息類的頭文件
hellowworld.pb.cc 包含消息類的實現
hellowworld.grpc.pb.h 聲明你生成的服務類的頭文件
hellowworld.grpc.pb.cc 包含服務類的實現
其中前兩個是protoc生成的,后兩個是插件生成的。
這些包括:
- 所有的填充,序列化和獲取我們請求和響應消息類型的 protocol buffer 代碼
- 名為 Greeter的類,包含
- 為了客戶端去調用定義在 Greeter服務的遠程接口類型(或者 存根 )
- 讓服務器去實現的兩個抽象接口,同時包括定義在 Greeter中的方法。
詳細見:https://doc.oschina.net/grpc?t=57966
3. 創建C++項目
4. 設置頭文件
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中找到的。
庫文件
libprotobuf.lib;
grpc.lib;
gpr.lib;
grpc++.lib;
Ws2_32.lib;
6. 編譯C++項目
將gRPC的C++ example的代碼拷貝到我們剛創建的項目中,編譯,出現一些error:
錯誤A:
error C1189: #error :"Please compile grpc with _WIN32_WINNT of at least 0x600 (aka Windows Vista)"port_platform.h 59 Server_Cpp
解決:在項目屬性中的Preprocessor Definitions
中添加_WIN32_WINNT=0x600
錯誤B:
error C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe - this call relies on the caller to check ****
解決:在項目屬性中的Preprocessor Definitions中添加
_SCL_SECURE_NO_WARNINGS
_CRT_SECURE_NO_WARNINGS
錯誤C:
error LNK2038: mismatch detected for 'RuntimeLibrary': value
解決:只需要主程序和靜態庫都采用同一種Runtime Libray編譯即可。
在項目屬性C/C++中的 代碼生成 的 運行庫 選擇 Multi-threaded(/MT)
OpenSSl我是添加的NuGet庫來解決的
點擊項目右鍵=>管理NuGet程序包
點擊瀏覽=>輸入grpc.dependencies.openssl
找到grpc.dependencies.openssl =>點擊安裝(我是用的v1.0.204.1版本)
完成,
編輯通過
c++生成helloworld client程序
依照
c++生成helloworld服務器程序
流程,
說明:
附上一個編譯好的版本,里面帶了測試程序(helloworld)
http://download.csdn.NET/detail/xie1xiao1jun/9630779