grpc vs2015編譯

獲取gRPC源碼

gRPC是開源框架,項目代碼在github上,因此首先要安裝github。
github安裝後,在指定文件夾中,執行git命令就能夠獲取gRPC的全部源碼。
git clone  https://github.com/grpc/grpc.git
cd grpc
git submodule update --init
 
step1. 編譯protobuf

參考readme用CMAKE生成工程文件,編譯便可。首先打開vs2015開發人員命令提示符窗口,切換到對應的protobuf目錄css

具體步驟:nginx

1:cd protobufc++

2: Git clone -b release-1.7.0 https://github.com/google/googlemock.git gmockgit

3:cd gmockgithub

4:git clone -b release-1.7.0 https://github.com/google/googletest.git gtestsql

5:cd ..\cmakeruby

6:mkdir build & cd build & mkdir install網絡

7:mkdir debug & cd debug多線程

8:cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=../install ../..框架

9:nmake && nmake install

10:生成完成,在install目錄下面有對應的Lib文件。在cmake目錄下面mkdir debug,而後把install/lib目錄下的全部庫文件拷貝的debug路徑,並把後綴d去掉。例如protobuf生成的庫名稱爲libprotocd.lib,應該更名成libprotoc.lib。其餘的依次類推。後面編譯grpc會用到這些庫。

 

step2. 編譯grpc,grpc_protoc_plugin:在vsprojects裏有建好的工程文件,下載nuget.exe,用於依賴包的網絡下載。主要是依賴於openssl和zlib庫。在編譯grpc時,出現編譯boringssl,出現不少錯誤,能夠把工程移除

具體步驟:

1:cd vsprojects

2:nuget restore grpc.sln

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

grpc庫生成成功。

4:編譯grpc_cpp_plugin,執行命令:msbuild grpc_protoc_plugins.sln /p:Configuration=Debug

grpc_cpp_plugin.exe插件編譯成功


用vs2015編譯基本不會遇到什麼問題,除了:grpc_cpp_plugin依賴libprotoc.lib,而protobuf生成的庫名稱爲libprotocd.lib,這塊須要手動改一下

step3. zlib 參考readme

step4編譯helloworld

1: cd grpc/example/protos

2: 把proto.exe(protobuf工程生成,在install/bin目錄下)、helloworld.proto拷貝到vsprojects\debug路徑下面。此時在vsprojects\debug路徑下面執行命令

 

[cpp]  view plain  copy
 
  1. protoc.exe -I=. --grpc_out=. --plugin=protoc-gen-grpc=.\grpc_cpp_plugin.exe helloworld.proto  
  2. protoc.exe -I=. --cpp_out=. helloworld.proto  
生成了兩套文件

 

 

[cpp]  view plain  copy
 
  1. hellowworld.pb.h  
  2. hellowworld.pb.cc  
  3. hellowworld.grpc.pb.h  
  4. hellowworld.grpc.pb.cc  
其中前兩個是protoc生成的,後兩個是插件生成的

 

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




 

工程配置:
頭文件包含路徑:
[cpp]  view plain  copy
 
  1. $(SolutionDir)..\..\grpc\third_party\protobuf\src;$(SolutionDir)..\..\grpc\include;$(ProjectDir)..\protobuf;%(AdditionalIncludeDirectories)  
預處理器設定
[cpp]  view plain  copy
 
  1. _DEBUG;_WIN32_WINNT=0x600;_SCL_SECURE_NO_WARNINGS;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)  

代碼生成-->運行庫設置爲 "多線程調試 (/MTd)"
 
附件庫目錄設定
[cpp]  view plain  copy
 
  1. $(OutDir);$(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)  
 
附加依賴項設定
libprotobuf.lib;grpc.lib;gpr.lib;grpc++.lib;Ws2_32.lib;libeay32.lib;ssleay32.lib;zlib.lib;%(AdditionalDependencies)
 
編譯工程文件,順利的話就能夠生成對應的exe了。
將gRPC的C++ example的代碼拷貝到咱們剛建立的項目中,編譯,出現一些error:

錯誤A:

Error 2 error C1189:#error :"Please compile grpc with _WIN32_WINNT of at least 0x600 (aka Windows Vista)" d:\projects_framework\grpc\include\grpc\impl\codegen\port_platform.h 59 1 Server_Cpp

解決:在項目屬性中的Preprocessor Definitions中添加_WIN32_WINNT=0x600

錯誤B:

Error    1    error C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators' c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 2132 1 Server_Cpp

解決:在項目屬性中的Preprocessor Definitions中添加

_SCL_SECURE_NO_WARNINGS _CRT_SECURE_NO_WARNINGS

錯誤C:

error LNK2038: mismatch detected for 'RuntimeLibrary': value

解決:只須要主程序和靜態庫都採用同一種Runtime Libray編譯便可。
在項目屬性C/C++中的Code Generation的Runtime Library選擇Multi-threaded(/MT)

錯誤D:

Error 2 error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MTd_StaticDebug' in greeter_server.obj D:\Projects_My\gRPC\gRPC_cpp\Server_Cpp\libprotobuf.lib(generated_message_util.obj) Server_Cpp

解決:緣由是將debug的配置拷貝到release時,將Preprocessor Definitions的_DEBUG也考到release中了,去掉_DEBUG便可

錯誤E:

Error 113 error LNK2001: unresolved external symbol _SSL_state D:\Projects_My\gRPC\gRPC_cpp\Server_Cpp\grpc.lib(ssl_transport_security.obj) Server_Cpp

解決:添加openssl的lib(用v120,多是vs的版本號),具體用v1xx能夠根據本身的vs版原本對應

 

D:\XXXX\grpc\vsprojects\packages\grpc.dependencies.openssl.1.0.204.1\build\native\lib\v120\Win32\Release\static

 

庫文件:

libeay32.lib ssleay32.lib

錯誤F:

Error    2 error LNK2005: "private: __thiscall type_info::type_info(class type_info const &)" (??0type_info@@AAE@ABV0@@Z) already defined in LIBCMT.lib(typinfo.obj) D:\Projects_My\gRPC\gRPC_cpp\Server_Cpp\msvcrt.lib(ti_inst.obj) Server_Cpp

解決:
Project Settings:
-> Configration Properties -> Linker -> Input -> Ignore Specific Library: LIBCMT.lib

在編譯的時候,有能夠還會遇到找不到Grpc內的某個符號,好比Channel、Complete_quque等, 如下以channel爲例說明解決方法:

GRPC編譯grpc++.lib時,生成的Channel.obj會被覆蓋(緣由沒查出來,大概是由於grpc.lib編譯時一樣會生成channel.obj)

 刪掉obj目錄中C:\path\to\grpc\vsprojects\IntDir\grpc++中的channel.obj,而後在工程裏「生成」(不要從新生成)

相關文章
相關標籤/搜索