本文共 4702 字,大约阅读时间需要 15 分钟。
参考readme用CMAKE生成工程文件,编译即可。首先打开vs2015开发人员命令提示符窗口,切换到对应的protobuf目录
具体步骤:
1:cd protobuf
2: clone -b release-1.7.0 https://github.com/google/googlemock. gmock
3:cd gmock
4:git clone -b release-1.7.0 https://github.com/google/googletest.git gtest
5:cd ..\cmake
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路径下面执行命令
3:在vsprojects目录创建空的WIN32 C++工程grpc_helloworld.sln,在目录grpc_helloworld下面有两个文件夹。目录结构如下图
解决:在项目属性中的Preprocessor Definitions
中添加_WIN32_WINNT=0x600
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
error LNK2038: mismatch detected for 'RuntimeLibrary': value
解决:只需要主程序和静态库都采用同一种Runtime Libray编译即可。
在项目属性C/C++中的Code Generation的Runtime Library选择Multi-threaded(/MT)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即可
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.libssleay32.lib
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,然后在工程里“生成”(不要重新生成)