天天看點

Windows下gRPC安裝小記

layout: post

title: “Windows下gRPC安裝小記”

subtitle: “在坑裡 再次打滾的經曆。。。”

date: “2016-07-12”

author: “cj”

tags:

proto3

protobuf

gRPC

windows

c++

搞定了 proto3後,開始搞 gRPC。又是一個巨坑。

從 github clone 項目之後,在 vsprojects 中打開 grpc.sln,編譯 gpr, gprc, gprc_unsecure, gprc++_unsecure 幾個項目,生成 lib 庫。因為用不了 boringsll,隻能用未加密的版本。期間小坑無數,每個項目配置都得是 Mt 或 Mtd。

再打開 grpc_protoc_plugin.sln,編譯 grpc_plugin_support, gprc_cpp_plugin 兩個項目。會生成 grpc_cpp_plugin.exe 檔案,可以放在 protoc 的路徑下,并加到系統路徑中,友善使用。

搞定前提條件後,到 example/protos 檔案夾中,先生成 protoc 的 pb:

protoc --cpp_out=. helloworld.proto
      

會生成helloworld.pb.h 和 helloworld.pb.cc兩個檔案。

再使用 grpc 插件生成 gprc 的 pb:

protoc --grpc_out=. --plugin=protoc-gen-grpc="your-grpc-cpp-plugin-exe-path" helloworld.proto
      

生成 helloworld.grpc.pb.h 和 helloworld.grpc.pb.cc 兩個檔案。

将上述4個檔案添加到建立的示例工程中,在工程屬性的 c/c++ Additional Include Directories 中添加 protobuf3庫中的 src 檔案夾路徑和 grpc 庫中 include 檔案夾路徑。在 Additional Library Directories 中添加 protobuf 和 grpc 中 vsprojects 的輸出路徑,或 debug 或 release 。在 Linker->Input 的 Additional Dependencies 中添加如下 lib 庫:

libprotobuf.lib
z.lib
gpr.lib
grpc_unsecure.lib
grpc++_unsecure.lib

      

編譯,我去又是一堆連結錯誤。不怕!至少說明代碼沒問題就是配置的事(廢話,Google 的示例代碼有什麼問題)。分析示例項目的連結錯誤,一般都是 unresolved external symbol,意思就是頭檔案裡有聲明,但找不到實作。按照我上一篇配置 proto3的經驗,繼續找沒加到工程中的 c 或 cc 檔案。

這些都搞定後,還有兩個坑。。。

一個是示例項目的屬性必須設定為 Mtd 或 Mt,一個是如果項目使用了預編譯頭,要在生成的 pb.cc 檔案的第一行非注釋行添加

#include "stdafx.h"