VS2015編譯grpc–不依賴GIT


VS2015編譯grpc–不依賴GIT

前言

編譯grpc時會遇到很多問題,耗時長且不一定能成功,其中牆和github下載慢的問題最是惱人。grpc master依賴https://boringssl.googlesource.com/boringssl,需要翻牆,grpc tag v1.0.1的依賴項全在github上,不需要翻牆。另外,我們可以直接下載打包好的代碼,不需要使用git的蝸牛下載。

編譯說明

操作系統:Win7 64位 
VS版本:Visual Studio 2015 
GRPC版本:tag v1.0.1 
CMake

編譯步驟

1、下載

1.1 在 https://github.com/grpc/grpc/tree/v1.0.1 上下載 grpc-1.0.1.zip 文件(下載路徑:https://github.com/grpc/grpc/archive/v1.0.1.zip)文件,將文件解壓到 grpc-1.0.1。 
1.2 切換到 third_party 目錄,分別下載依賴的其它項目,下達完成之后將依賴項分別解壓到 third_party 下的對應目錄下。 
如 在2017.01.17日,tag v1.0.1 對應的依賴項鏈接如下: 
https://codeload.github.com/grpc/grpc/zip/v1.0.1 
https://codeload.github.com/google/boringssl/zip/c880e42ba1c8032d4cdde2aba0541d8a9d9fa2e9 
https://codeload.github.com/gflags/gflags/zip/05b155ff59114735ec8cd089f669c4c3d8f59029 
https://codeload.github.com/google/googletest/zip/c99458533a9b4c743ed51537e25989ea55944908 
https://codeload.github.com/google/protobuf/zip/1a586735085e817b1f52e53feec92ce418049f69 
https://codeload.github.com/madler/zlib/zip/50893291621658f355bc5b4d450a8d06a563053d

2、編譯protobuf

參考readme用CMAKE生成工程文件,編譯即可。

2.1、下載protobuf依賴項gmock和gtest 
下載gmock(https://github.com/google/googlemock/archive/release-1.7.0.zip)和gtest(https://github.com/google/googletest/archive/release-1.7.0.zip),分別解壓到grpc-1.0.1\third_party\protobuf\gmock 和 grpc-1.0.1\third_party\protobuf\gmock\gtest\ 目錄下。 
2.2、使用CMake編譯protobuf 
首先打開vs2015開發人員命令提示符窗口(使用 "VS2015 x86 本機工具命令提示符" 工具),切換到對應的protobuf目錄。

  1. cd grpc-1.0.1\third_party\protobuf\cmake
  2. mkdir build & cd build & mkdir install
  3. mkdir debug & cd debug
  4. cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=../install ../..
  5. nmake && nmake install

生成完成,在grpc-1.0.1\third_party\protobuf\cmake\build\install\lib 目錄下面有對應的Lib文件。在cmake目錄下面mkdir debug,然后把install/lib目錄下的所有庫文件拷貝的debug路徑,並把后綴d去掉。例如protobuf生成的庫名稱為libprotocd.lib,應該改名成libprotoc.lib。其他的依次類推。后面編譯grpc會用到這些庫。

3、編譯grpc,grpc_protoc_plugin

在vsprojects里有建好的工程文件,下載nuget.exe,用於依賴包的網絡下載。主要是依賴於openssl和zlib庫。在編譯grpc時,出現編譯boringssl,出現很多錯誤,可以把工程移除可以直接在 grpc.sln 文件中刪除 boringssl 工程。

3.1、下載nuget 
下載nuget.exe (路徑:https://nuget.org/nuget.exe ),並將期復制到 grpc-1.0.1\vsprojects\ 目錄下。 
3.2、使用nuget下載依賴包

  1. cd grpc-1.0.1\vsprojects
  2. nuget restore grpc.sln

3.3、編譯grpc 
打開 grpc-1.0.1\vsprojects\grpc.sln 文件,刪除boringssl工程,否則后面編譯grpc時會報錯。若不使用ssl,boringssl可以不編譯。

  1. msbuild grpc.sln /p:Configuration=Debug

grpc-1.0.1\vsprojects\Debug\ 目錄下會生成grpc相關文件。

3.3、編譯grpc_cpp_plugin

  1. msbuild grpc_protoc_plugins.sln /p:Configuration=Debug

grpc-1.0.1\vsprojects\Debug\ 目錄下會生成grpc_cpp_plugin相關文件。

4、編譯zlib

參考readme,使用cmake編譯。

  1. cd grpc-1.0.1\third_party\zlib\
  2. cmake CMakeLists.txt

再用 vs2015 打開 zlib.sln編譯即可,文件生成在 Debug 目錄。

grpc測試

1、根據helloworld.proto生成.h和.cc文件

復制proto.exe(protobuf工程生成,路徑:grpc-1.0.1\third_party\protobuf\cmake\build\install\bin\protoc.exe)、grpc_cpp_plugin.exe(grpc_cpp_plugin工程生成,路徑:grpc-1.0.1\vsprojects\Debug\grpc_cpp_plugin.exe)到 grpc-1.0.1\example\protos目錄下。

  1. cd grpc-1.0.1\examples\protos\
  2. protoc.exe -I=. --grpc_out=. --plugin=protoc-gen-grpc=.\grpc_cpp_plugin.exe helloworld.proto
  3. protoc.exe -I=. --cpp_out=. helloworld.proto

在grpc-1.0.1\examples\protos\目錄下會生成:hellowworld.pb.h、hellowworld.pb.cc、hellowworld.grpc.pb.h、hellowworld.grpc.pb.cc。

2、創建grpc_helloworld工程

在vsprojects目錄創建空的WIN32 C++工程grpc_helloworld.sln,在目錄grpc_helloworld下面有兩個文件夾。目錄結構如下圖:

grpc-1.0.1\vsprojects\grpc_helloworld.sln 
grpc-1.0.1\vsprojects\grpc_helloworld\grpc_helloworld.vcxproj 
grpc-1.0.1\vsprojects\grpc_helloworld\grpc_helloworld.vcxproj.filters 
grpc-1.0.1\vsprojects\grpc_helloworld\protobuf\helloworld.pb.h 
grpc-1.0.1\vsprojects\grpc_helloworld\protobuf\helloworld.pb.cc 
grpc-1.0.1\vsprojects\grpc_helloworld\protobuf\helloworld.grpc.pb.h 
grpc-1.0.1\vsprojects\grpc_helloworld\protobuf\helloworld.grpc.pb.cc 
grpc-1.0.1\vsprojects\grpc_helloworld\client\greeter_client.cc

工程配置: 
頭文件包含路徑: 
$(SolutionDir)..\third_party\protobuf\src;$(SolutionDir)..\include;$(ProjectDir)protobuf;%(AdditionalIncludeDirectories)

預處理器設定: 
_DEBUG;_WIN32_WINNT=0x600;_SCL_SECURE_NO_WARNINGS;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)

代碼生成–>運行庫 設置為 "多線程調試 (/MTd)"

附件庫目錄設定: 
$(OutDir);$(SolutionDir)..\third_party\protobuf\cmake\debug;$(SolutionDir)packages\grpc.dependencies.openssl.1.0.204.1\build\native\lib\v140\Win32\Debug\dynamic;$(SolutionDir)packages\grpc.dependencies.zlib.1.2.8.10\build\native\lib\v140\Win32\Debug\dynamic\cdecl;%(AdditionalLibraryDirectories);$(SolutionDir)Debug

附加依賴項設定: 
libprotobuf.lib;grpc.lib;gpr.lib;grpc++.lib;Ws2_32.lib;libeay32.lib;ssleay32.lib;zlib.lib;%(AdditionalDependencies)

編譯工程文件,順利的話就可以生成對應的exe了。

對於 greeter_server.cc 可以使用類似方法創建新的工程並編譯。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM