c++生成helloworld服务器程序
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插件,生成方法参考上文。
创建一个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生成的,后两个是插件生成的。
这些包括:
- 所有的填充,序列化和获取我们请求和响应消息类型的 protocol buffer 代码
- 名为 Greeter的类,包含
- 为了客户端去调用定义在 Greeter服务的远程接口类型(或者 存根 )
- 让服务器去实现的两个抽象接口,同时包括定义在 Greeter中的方法。
3. 创建C++项目
4. 设置头文件
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中找到的。
库文件
libprotobuf.lib;
grpc.lib;
gpr.lib;
grpc++.lib;
Ws2_32.lib;
6. 编译C++项目
将gRPC的C++ example的代码拷贝到我们刚创建的项目中,编译,出现一些error:
错误A:
error C1189: #error :"Please compile grpc with _WIN32_WINNT of at least 0x600 (aka Windows Vista)"port_platform.h 59 Server_Cpp
解决:在项目属性中的Preprocessor Definitions
中添加_WIN32_WINNT=0x600
错误B:
error C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe - this call relies on the caller to check ****
解决:在项目属性中的Preprocessor Definitions中添加
_SCL_SECURE_NO_WARNINGS
_CRT_SECURE_NO_WARNINGS
错误C:
error LNK2038: mismatch detected for 'RuntimeLibrary': value
解决:只需要主程序和静态库都采用同一种Runtime Libray编译即可。
在项目属性C/C++中的 代码生成 的 运行库 选择 Multi-threaded(/MT)
点击项目右键=>管理NuGet程序包
点击浏览=>输入grpc.dependencies.openssl
找到grpc.dependencies.openssl =>点击安装(我是用的v1.0.204.1版本)
完成,
编辑通过
c++生成helloworld client程序
依照
c++生成helloworld服务器程序
流程,
说明:
附上一个编译好的版本,里面带了测试程序(helloworld)
http://download.csdn.net/detail/xie1xiao1jun/9630779