且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

编译协议缓冲区生成的源文件时,有没有更好的方法来解决警告?

更新时间:2023-11-25 11:38:58

您可以破解协议编译器的源代码,使其自动将编译指示注入到生成的文件中。

You can hack the source of the protoc compiler to have it inject the pragmas into the generated files automatically.

src/google/protobuf/compiler/cpp/cpp_file.cc in GenerateHeader(io :: Printer * printer)在第94行附近,将第一个 printer-> Print 调用更改为:

In src/google/protobuf/compiler/cpp/cpp_file.cc in GenerateHeader(io::Printer* printer) around line 94, change the first printer->Print call to:

  // Generate top of header.
  printer->Print(
    "// Generated by the protocol buffer compiler.  DO NOT EDIT!\n"
    "// source: $filename$\n"
    "\n"
    "#ifndef PROTOBUF_$filename_identifier$__INCLUDED\n"
    "#define PROTOBUF_$filename_identifier$__INCLUDED\n"
    "\n"
    "#ifdef _MSC_VER\n"
    "#  pragma warning(push)\n"
    "#  pragma warning(disable: 4127 4244 4267)\n"
    "#endif\n"
    "\n"
    "#include <string>\n"
    "\n",
    "filename", file_->name(),
    "filename_identifier", filename_identifier);

然后在第294行附近的相同功能,更改最后一个 printer-&gt ;打印调用:

Then at the end of the same function at around line 294, change the last printer->Print call to:

  printer->Print(
    "#ifdef _MSC_VER\n"
    "#  pragma warning(pop)\n"
    "#endif\n"
    "\n"
    "#endif  // PROTOBUF_$filename_identifier$__INCLUDED\n",
    "filename_identifier", filename_identifier);

现在,您只需要编译protoc目标并运行新的protoc.exe,即可​​在其中使用编译指示生成的标题。

Now you just need to compile the protoc target and run the new protoc.exe to have the pragmas in the generated headers.