我的這幾篇文章都是使用gRPC的example,不是直接編譯example,而是新建一個項目,從添加依賴,編譯example代碼,執行example。這樣做可以為我們創建自己的項目提供借鑒。如果對gRPC構建應用很熟悉,可以無視本系列文章。
目錄
上篇介紹了C#調用gRPC,C#項目比較幸運有NuGet幫助配置,C++項目就沒有那么幸運了。配置復雜,編譯問題也很多。我在這里把我遇到的情況都列出來,使大家在遇到相同的問題時,少走一些彎路。
1.定義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;
}
2. 生成訪問代碼
將proto.exe、helloworld.proto、grpc_cpp_plugin.exe拷貝到一個文件夾中,grpc_cpp_plugin.exe是gRPC的protoc插件,生成方法參考我的《編譯gRPC》一文。
創建一個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生成的,后兩個是插件生成的。
3. 創建C++項目
4. 設置頭文件
D:\XXXX\grpc\include;
D:\XXXX\grpc\third_party\protobuf\src;
$(ProjectDir)..\proto
三個頭文件目錄,一個是gRPC的頭文件,一個是protobuf的頭文件,一個是剛生成的的訪問類路徑。
5. 設置庫
路徑
D:\XXXX\grpc\third_party\protobuf\cmake\Release;
D:\XXXX\grpc\vsprojects\Release;
D:\XXXX\grpc\third_party\zlib\solution\Release;
D:\XXXX\grpc\vsprojects\packages\grpc.dependencies.openssl.1.0.204.1\build\native\lib\v120\Win32\Release\static
四個庫文件路徑:protobuf庫路徑、grpc庫路徑、zlib庫路徑、openssl庫路徑。
沒有使用boringssl,openssl的庫是從NuGet下載的package中找到的。
庫文件
libprotobuf.lib;
grpc.lib;
gpr.lib;
grpc++.lib;
Ws2_32.lib;
libeay32.lib;
ssleay32.lib;
zlibstatic.lib;
%(AdditionalDependencies)
6. 編譯C++項目
將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的版本號)
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
至此,C++的gRPC項目編譯通過。