項目地址:https://gitee.com/c15721527020/gRPCDemo/tree/master/HelloWorld
grpc編譯生成文件下載地址:https://gitee.com/c15721527020/gRPCDemo/blob/master/ccinstall.zip
1:grpc編譯階段
1:安裝 git cmake nasm
2:git clone https://github.com/grpc/grpc
3:cd grpc
4:git submodule update --init
這裏有可能會出現問題,因爲qiang 的原因,所以如果 third_party 安裝失敗,可以根據報錯提示,一個一個手動安裝,手動安裝的過程1:cd third_party
2:git clone git.xxxxxxxx.git(這裏根據報錯提示安裝,把https 切換成 http)
5:mkdir .build
6:cd .build 7:cmake .. -G "Visual Studio 16 2019" -DCMAKE_INSTALL_PREFIX=../ccinstall 這裏可以使用-DCMAKE_INSTALL_PREFIX指定安裝目錄
8:然後進入 .build目錄 打開 sln文件 然後 在visual studio 2019 中 點擊全部生成,然後再運行INSTALL目錄執行安裝
9:最終的結果 如圖
2:生成 proto
編譯完成會生成install文件夾,後面所有用到的頭文件和lib文件都在 這個文件夾下面
文件夾層級結構如圖:
進入 bin文件夾,打開cmd命令行,執行如下操作:
protoc --grpc_out=. --plugin=protoc-gen-grpc=grpc_cpp_plugin.exe helloworld.proto
protoc --cpp_out=. helloworld.proto
生成目錄如下:
helloworld.proto文件如下
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;
}
3:項目創建
右鍵項目屬性
獲取lib文件夾下所有lib文件:
dir *.lib /s /b > lists.txt
複製第一步驟生成的
helloworld.grpc.pb.h helloworld.pb.h helloworld.grpc.pb.cc helloworld.pb.cc 文件到新創建的項目目錄,如圖
創建greeter_client.cc, greeter_server.cc這兩個文件可以在grpc github上找到 路徑如下
然後將文件全部包含在項目中
分別註釋 greeter_client.cc 的main函數和 greeter_server.cc 的main函數進行編譯即可
運行結果