无法在 Visual C++ 中禁用警告

Cannot disable warning in Visual C++

我需要禁用来自使用外部库 ProtocolBuffers 的项目目录的所有警告,该库会生成自己的警告。

这些是我收到的 151 条警告中的一部分:

my.pb.cc: warning C4512: 'google::protobuf::FatalException' : assignment operator could not be generated
my.pb.cc: warning C4244: 'initializing' : conversion from '__int64' to 'int', possible loss of data
my.pb.cc: warning C4125: decimal digit terminates octal escape sequence
my.pb.cc: warning C4127: conditional expression is constant
my.pb.cc
xutility(2132): 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.

其中 xutility 是一个 Visual Studio 包含文件。

如果我在 CMakeLists.txt 文件中添加这些行:

# Use a lower warning level threshold for autogenerated files
set(PROTOBUF_FILES_PATH "${CMAKE_CURRENT_BINARY_DIR}/src/*.pb.*")
file(GLOB PROTOBUF_FILES ${PROTOBUF_FILES_PATH})
set_source_files_properties(${PROTOBUF_FILES} PROPERTIES COMPILE_FLAGS /W2)

我从生成的 pb.cc 文件中收到所有警告,但我仍然有 15 个来自 ProtoBuf 内部代码的警告,类型为:

my.pb.cc
xutility(2132): 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.

考虑到我无法修改 ProtoBuf 本身,我如何禁用这些最后的警告,可能调整我的 CMakeLists.txt 文件添加 -D_SCL_SECURE_NO_WARNINGS 选项,仅针对扩展名为 *.pb.* 的文件?

在不太了解 xutility 或 Protobuf 如何使用它的情况下,我建议尝试扩展您的 set_source_files_properties() 调用以包含 COMPILE_DEFINITIONS:

set_source_files_properties(${PROTOBUF_FILES} PROPERTIES 
    COMPILE_DEFINITIONS _SCL_SECURE_NO_WARNINGS 
    COMPILE_FLAGS /W2
)