protoc-gen-grpc-c++插件生成

在正片开始前,我有必要吐槽一句。。。

很复杂的经历,在配置nvidia::inferenceserver客户端的时候,不得不包含上~~~某某grpc.ph.h的头文件,

然后很“欣慰”的是并没有,因为一般这种东西源码是不会有的,所以我很习惯的先百度看看有没有哪位大佬有把我标题的插件分享下,又很“高兴”的是没有看到。

所以只能自己动手做这个插件了,然后我就看到了这位大佬写的文章

首先先谢谢这位老哥。

废话不多说,进入正题。

在开始配置之前,您需要阅读下我转发的这片文章,理解下大概意思即可。

然后下载所需文件,直接百度搜grpc github下载解压即可。

根据大佬的指导,新建vs2015(至少vs2013)工程,配置环境。

将所需的文件添加到源文件内

不出所料的你会有很多链接错误和函数不安全使用错误。

解决方案:一、链接错误,很明显是缺库,后续我会把所需库(lib)发到资源上,大家可以下载。

二、函数不安全使用

添加如下两个命令:

_CRT_SECURE_NO_WARNINGS
_CRT_NONSTDC_NO_DEPRECATE

之后就是源码编写问题了:

第一步,把cpp_plugin.cc的main函数注释掉

第二部,main.cpp改写我直接上源码,大家自己去看和原来的有什么不一样。

// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Author: kenton@google.com (Kenton Varda)

#include <google/protobuf/compiler/command_line_interface.h>
#include <google/protobuf/compiler/cpp/cpp_generator.h>

#ifndef OPENSOURCE_PROTOBUF_CPP_BOOTSTRAP
#include <google/protobuf/compiler/python/python_generator.h>
#include <google/protobuf/compiler/java/java_generator.h>
#endif  // ! OPENSOURCE_PROTOBUF_CPP_BOOTSTRAP

#ifndef OPENSOURCE_PROTOBUF_CPP_BOOTSTRAP
#include <google/protobuf/compiler/javanano/javanano_generator.h>
#include <google/protobuf/compiler/php/php_generator.h>
#include <google/protobuf/compiler/ruby/ruby_generator.h>
#include <google/protobuf/compiler/csharp/csharp_generator.h>
#include <google/protobuf/compiler/objectivec/objectivec_generator.h>
#include <google/protobuf/compiler/js/js_generator.h>
#endif  // ! OPENSOURCE_PROTOBUF_CPP_BOOTSTRAP

// GRPC的service相关的生成器位于grpc/src/compiler目录下,
// 主要实现grpc::protobuf::compiler::CodeGenerator接口,
// 这里以C++为例
// grpc/src/compiler/cpp_plugin.cc

#include "cpp_plugin.cc"
int main(int argc, char* argv[]) {

  google::protobuf::compiler::CommandLineInterface cli;
  cli.AllowPlugins("protoc-");

  // Proto2 C++
  /*google::protobuf::compiler::cpp::CppGenerator cpp_generator;
  cli.RegisterGenerator("--cpp_out", "--cpp_opt", &cpp_generator,
                        "Generate C++ header and source.");*/
  CppGrpcGenerator generator;
  cli.RegisterGenerator("--grpc_out", "--grpc_opt", &generator,
	  "Generate C++ header and source.");
#ifdef OPENSOURCE_PROTOBUF_CPP_BOOTSTRAP
  // Proto2 Java
  google::protobuf::compiler::java::JavaGenerator java_generator;
  cli.RegisterGenerator("--java_out", &java_generator,
                        "Generate Java source file.");
#endif  // !OPENSOURCE_PROTOBUF_CPP_BOOTSTRAP


#ifdef OPENSOURCE_PROTOBUF_CPP_BOOTSTRAP
  // Proto2 Python
  google::protobuf::compiler::python::Generator py_generator;
  cli.RegisterGenerator("--python_out", &py_generator,
                        "Generate Python source file.");

  // Java Nano
  google::protobuf::compiler::javanano::JavaNanoGenerator javanano_generator;
  cli.RegisterGenerator("--javanano_out", &javanano_generator,
                        "Generate Java Nano source file.");

  // PHP
  google::protobuf::compiler::php::Generator php_generator;
  cli.RegisterGenerator("--php_out", &php_generator,
                        "Generate PHP source file.");

  // Ruby
  google::protobuf::compiler::ruby::Generator rb_generator;
  cli.RegisterGenerator("--ruby_out", &rb_generator,
                        "Generate Ruby source file.");

  // CSharp
  google::protobuf::compiler::csharp::Generator csharp_generator;
  cli.RegisterGenerator("--csharp_out", "--csharp_opt", &csharp_generator,
                        "Generate C# source file.");

  // Objective C
  google::protobuf::compiler::objectivec::ObjectiveCGenerator objc_generator;
  cli.RegisterGenerator("--objc_out", "--objc_opt", &objc_generator,
                        "Generate Objective C header and source.");

  // JavaScript
  google::protobuf::compiler::js::Generator js_generator;
  cli.RegisterGenerator("--js_out", &js_generator,
                        "Generate JavaScript source.");
#endif  // !OPENSOURCE_PROTOBUF_CPP_BOOTSTRAP

  return cli.Run(argc, argv);
}

大概就是这样。接着用cmd命令行操作

例如protoc-gen-plugin --grpc_out=./ 某某.proto

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值