VS2015编译grpc–不依赖GIT

VS2015编译grpc–不依赖GIT

前言

编译grpc时会遇到很多问题,耗时长且不一定能成功,其中墙和github下载慢的问题最是恼人。grpc master依赖https://boringssl.googlesource.com/boringssl,需要FQ,grpc tag v1.0.1的依赖项全在github上,不需要FQ。另外,我们可以直接下载打包好的代码,不需要使用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 可以使用类似方法创建新的工程并编译。

posted @ 2017-02-08 10:33  lontoken  阅读(3106)  评论(0编辑  收藏  举报