Gan*_*Yin 16 c++ 64-bit protocol-buffers visual-c++
对于一个简单的proto文件:
message Person {
required int32 id = 1;
required string name = 2;
optional string email = 3;
}
它是由protoc.exe编译的,结果用于一个简单的测试项目,除了包括protoc生成的文件之外什么也没做.
我正在使用msvc10来构建测试项目(x64),然后它给了我很多警告:
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/02/20 ]
工作方案:
你可以破解protoc编译器的源代码,让它自动将pragma注入到生成的文件中.
在第94行附近的src/google/protobuf/compiler/cpp/cpp_file.cc中GenerateHeader(io::Printer* printer),将第一个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);
Run Code Online (Sandbox Code Playgroud)
然后在第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);
Run Code Online (Sandbox Code Playgroud)
现在您只需要编译protoc目标并运行新的protoc.exe以在生成的头文件中包含pragma.
小智 7
一个简单的方法是使用包装头来包含生成的 protobuf 头:
#ifndef MESSAGES_WRAPPER_H
#define MESSAGES_WRAPPER_H
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4018 4100 4267)
#endif
#include "messages.pb.h"
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#endif // MESSAGES_WRAPPER_H
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4447 次 |
| 最近记录: |