gpt4 book ai didi

c++ - 编译 Protocol Buffer 生成的源文件时,是否有更好的方法来解决警告?

转载 作者:太空宇宙 更新时间:2023-11-04 12:53:42 27 4
gpt4 key购买 nike

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

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   testProtobufWarning 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   testProtobufWarning 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   testProtobufWarning 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   testProtobufWarning 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   testProtobufWarning 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   testProtobufWarning 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   testProtobufWarning 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

有什么好的方法可以解决所有这些警告吗?任何建议将不胜感激。

附言。 libprotobuf 项目本身可以在没有任何警告的情况下由 msvc10 干净编译。

[编辑 2013/02/20]

工作解决方案:

  1. 为那些 protoc 生成的 .cc 文件设置属性:
    配置属性 -> c/c++ -> 高级 -> 禁用特定警告

最佳答案

一种简单的方法是使用包装器 header 来包含生成的 protobuf header :

#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

关于c++ - 编译 Protocol Buffer 生成的源文件时,是否有更好的方法来解决警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47713069/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com