天天看点

windwos grpc 编译此文档是windwos grpc c++ 编译 ,基于 vs2015 编译完成 获取gRPC源码 获取gRPC的依赖组件 vs工程调试 编译protobuffer c++生成helloworld服务器程序 1.定义proto 2. 生成访问代码 3. 创建C++项目 4. 设置头文件 5. 设置库 6. 编译C++项目 c++生成helloworld client程序

gRPC是开源框架,项目代码在github上,所以首先要安装github。

虽然在github的gRPC主页上提供了源代码打包下载,但是gRPC的依赖组件就无法自动获取了。

(第三方库目录在third_party下面)

用vs2015打开

vsprojects/grpc.sln

1,删除工程boringssl

2,在\grpc\third_party\zlib\gzguts.h

中将

#ifdef _WIN32  

#  include <stddef.h>  

#endif  

改成

#pragma warning(disable:4996)  

去掉警告。

完成:可以编译

说明:NuGet有还原库,需要等待完成

gRPC依赖protobuffer进行消息编码,因此需要依赖protobuffer。(详细见:grpc\third_party\protobuf\cmake\README.md)

需要git,cmake支持

cmd打开vs命令行工具(Windows Desktop Command Prompts/VS2015 x64 x86 兼容工具命令提示符)

cd 到grpc目录

cd protobuf  

git clone -b release-1.7.0 https://github.com/google/googlemock.git gmock  

cd gmock  

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

cd ..\cmake  

mkdir build & cd build  

mkdir release & cd release  

cmake -G "NMake Makefiles" ^  

     -DCMAKE_BUILD_TYPE=Release ^  

     -DCMAKE_INSTALL_PREFIX=../../../../install ^  

     ../..  

cd ..  

mkdir debug & cd debug  

     -DCMAKE_BUILD_TYPE=Debug ^  

mkdir solution & cd solution  

cmake -G "Visual Studio 14 2015 Win64" ^  

打开grpc\third_party\protobuf\cmake\build\solution下protobuf.sln

编译成功

生成文件:

protoc.exe

libprotobuf.lib

libprotoc.lib

后续有用到

将编译好的Debug,Release文件夹拷贝到grpc\third_party\protobuf\cmake\目录下(Debug下面的lib文件后边的d需要去掉)

打开grpc\vsprojects\grpc_protoc_plugins.sln编译生成可执行文件

完成

生成文件:

grpc_cpp_plugin.exe

grpc_csharp_plugin.exe

grpc_node_plugin.exe

grpc_objective_c_plugin.exe

grpc_python_plugin.exe

grpc_ruby_plugin.exe

(详细见:grpc\examples\protos\helloworld.proto)

将proto.exe、helloworld.proto、grpc_cpp_plugin.exe拷贝到一个文件夹中,grpc_cpp_plugin.exe是gRPC的protoc插件,生成方法参考上文。

创建一个bat文件,包含以下命令:

生成了两套文件

其中前两个是protoc生成的,后两个是插件生成的。

这些包括:

所有的填充,序列化和获取我们请求和响应消息类型的 protocol buffer 代码

名为 Greeter的类,包含

为了客户端去调用定义在 Greeter服务的远程接口类型(或者 存根 )

让服务器去实现的两个抽象接口,同时包括定义在 Greeter中的方法。

详细见:https://doc.oschina.net/grpc?t=57966

依照grpc\examples\cpp\helloworld下client,server例子

greeter_async_client.cc

greeter_async_client2.cc

greeter_async_server.cc

greeter_client.cc

greeter_server.cc

将刚刚生成的文件放入工程源代码目录$(SolutionDir)并包含进工程

GrpcTest\GrpcTest\src\protobuf

将:grpc\include,grpc\third_party\protobuf\src中的文件拷贝到include目录(自己的库目录)

三个头文件目录,一个是刚生成的的访问类路径,一个是gRPC的头文件,一个是protobuf的头文件.

将:

grpc\third_party\protobuf\cmake\Release;

grpc\vsprojects\Release;

grpc\third_party\zlib\solution\Release;

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

放入lib目录(自己的lib库目录)

四个库文件路径:protobuf库路径、grpc库路径、zlib库路径、openssl库路径。

没有使用boringssl,openssl的库是从NuGet下载的package中找到的。

库文件

将gRPC的C++ example的代码拷贝到我们刚创建的项目中,编译,出现一些error:

解决:在项目属性中的<code>Preprocessor Definitions</code>中添加<code>_WIN32_WINNT=0x600</code>

解决:在项目属性中的Preprocessor Definitions中添加

解决:只需要主程序和静态库都采用同一种Runtime Libray编译即可。

在项目属性C/C++中的 代码生成 的 运行库 选择 Multi-threaded(/MT)

OpenSSl我是添加的NuGet库来解决的

点击项目右键=&gt;管理NuGet程序包

点击浏览=&gt;输入grpc.dependencies.openssl

找到grpc.dependencies.openssl =&gt;点击安装(我是用的v1.0.204.1版本)

完成,

编辑通过

依照

c++生成helloworld服务器程序

流程,

我使用的

两个测试来做的测试

说明:

<a href="http://download.csdn.net/detail/xie1xiao1jun/9630779" target="_blank">点击打开链接</a>

继续阅读