是否有一些更好的方法编制的协议缓存生成的源文件时,解决的警告?(Are there some bet

2019-07-04 03:08发布

对于一个简单的原型文件:

message Person {
  required int32 id = 1;
  required string name = 2;
  optional string email = 3;
}

它已经被编译并protoc.exe结果在还简单的测试项目,基本上没有什么,但包括protoc生成的文件中。

我使用的是msvc10建立测试项目(64位),然后给了我很多的警告:

Warning 1   warning C4244: 'return' : conversion from '__int64' to 'int', possible loss of data D:\Work\protobuf-trunk\src\google\protobuf\descriptor.h 1441    1   testProtobuf
...
Warning 11  warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data    D:\Work\protobuf-trunk\src\google\protobuf\unknown_field_set.h  142 1   testProtobuf
Warning 12  warning C4267: 'return' : conversion from 'size_t' to 'int', possible loss of data  D:\Work\protobuf-trunk\src\google\protobuf\unknown_field_set.h  237 1   testProtobuf
...
Warning 14  warning C4244: '=' : conversion from '__int64' to 'int', possible loss of data  D:\Work\protobuf-trunk\src\google\protobuf\io\coded_stream.h    902 1   testProtobuf
Warning 15  warning C4244: 'return' : conversion from '__int64' to 'int', possible loss of data D:\Work\protobuf-trunk\src\google\protobuf\io\coded_stream.h    1078    1   testProtobuf
Warning 16  warning C4267: 'argument' : conversion from 'size_t' to 'google::protobuf::uint32', possible loss of data   D:\Work\protobuf-trunk\src\google\protobuf\wire_format_lite_inl.h   663 1   testProtobuf
...
Warning 19  warning C4267: 'return' : conversion from 'size_t' to 'int', possible loss of data  D:\Work\protobuf-trunk\src\google\protobuf\wire_format_lite_inl.h   739 1   testProtobuf
Warning 20  warning C4267: 'argument' : conversion from 'size_t' to 'google::protobuf::uint32', possible loss of data   D:\Work\protobuf-trunk\src\google\protobuf\wire_format_lite_inl.h   742 1   testProtobuf
Warning 21  warning C4267: 'return' : conversion from 'size_t' to 'int', possible loss of data  D:\Work\protobuf-trunk\src\google\protobuf\wire_format_lite_inl.h   743 1   testProtobuf
Warning 22  warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data    D:\Work\testProtobuf\testProtobuf\person.pb.cc  211 1   testProtobuf
...
Warning 28  warning C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xutility 2239    1   testProtobuf
Warning 29  warning C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xutility 2239    1   testProtobuf

有没有办法解决所有这些警告什么好办法? 任何建议将不胜感激。

PS。 该libprotobuf项目本身可以通过清洁msvc10没有任何警告编译。

[ 编辑2013年2月20日 ]

工作液:

  1. 对于那些protoc产生.cc的文件,设置属性:
    配置属性 - > C / C ++ - >高级 - >禁用特定警告

Answer 1:

你能砍的protoc编译器的源,系统将自动注入编译指示到生成的文件。

在SRC /谷歌/ protobuf的/编译器/ CPP / cpp_file.cc在GenerateHeader(io::Printer* printer)围绕线94,改变第一printer->Print调用:

  // 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->Print呼吁:

  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有在生成的标头的编译指示。



文章来源: Are there some better ways to address warnings when compiling protocol buffer generated source file?