Visual Studio 2019标准库生成警告



我想使项目具有最大的警告级别,并将警告视为错误。

有我的CmakeLists.txt文件:

cmake_minimum_required (VERSION 3.17.0 FATAL_ERROR)
set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} /Wall /WX /EHcs")
project(pedantic)
set(CMAKE_CXX_STANDARD 17)
add_executable (pedantic main.cpp)

有我的main.cpp文件:

#include <string>
#include <iostream>
int main()
{
std::string str = "abc";
std::cout << str;
return 0;
}

我使用以下命令生成解决方案:

cmake ../ -G "Visual Studio 16 2019" -DCMAKE_BUILD_TYPE=Release

输出为:

-- Configuring done
-- Generating done
-- Build files have been written to: D:/Programming/Progz/C++/pedantic/build_x64_VS2019

之后我构建了这个程序:

cmake --build . --config Release

我得到了很多警告:

C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.29.30133includeostream(118): error C2220: the following warning is treated as an error [D:ProgrammingProgzC++pedanticbuild_x64_VS2019pedantic.vcxproj]
C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.29.30133includeostream(118): warning C4710: 'void __cdecl std::basic_ostream<char,struct std::char_traits<char> >::_Osfx(void) __ptr64': function not inlined [D:ProgrammingProgzC++pedanticbuild_x64_VS2019pedantic.vcxproj]
C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.29.30133includeostream(103): warning C4710: 'class std::basic_ostream<char,struct std::char_traits<char> > & __ptr64 __cdecl std::basic_ostream<char,struct std::char_traits<char> >::flush(void) __ptr64': function not inlined [D:ProgrammingProgzC++pedanticbuil
d_x64_VS2019pedantic.vcxproj]
C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.29.30133includeexception(316): warning C4710: '__cdecl std::bad_array_new_length::bad_array_new_length(void) __ptr64': function not inlined [D:ProgrammingProgzC++pedanticbuild_x64_VS2019pedantic.vcxproj]
C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.29.30133includexmemory(131): warning C4710: 'void __cdecl std::_Throw_bad_array_new_length(void)': function not inlined [D:ProgrammingProgzC++pedanticbuild_x64_VS2019pedantic.vcxproj]
C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.29.30133includexmemory(229): warning C4711: function 'void * __ptr64 __cdecl std::_Allocate_manually_vector_aligned<struct std::_Default_allocate_traits>(unsigned __int64)' selected for automatic inline expansion [D:ProgrammingProgzC++pedanticbuild_x64_VS2019
pedantic.vcxproj]
C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.29.30133includexmemory(229): warning C4711: function 'void * __ptr64 __cdecl std::_Allocate_manually_vector_aligned<struct std::_Default_allocate_traits>(unsigned __int64)' selected for automatic inline expansion [D:ProgrammingProgzC++pedanticbuild_x64_VS2019
pedantic.vcxproj]
C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.29.30133includexstring(511): warning C4710: 'int __cdecl std::basic_streambuf<char,struct std::char_traits<char> >::sputc(char) __ptr64': function not inlined [D:ProgrammingProgzC++pedanticbuild_x64_VS2019pedantic.vcxproj]
C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.29.30133includexstring(519): warning C4710: '__int64 __cdecl std::basic_streambuf<char,struct std::char_traits<char> >::sputn(char const * __ptr64,__int64) __ptr64': function not inlined [D:ProgrammingProgzC++pedanticbuild_x64_VS2019pedantic.vcxproj]
C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.29.30133includexstring(523): warning C4710: 'int __cdecl std::basic_streambuf<char,struct std::char_traits<char> >::sputc(char) __ptr64': function not inlined [D:ProgrammingProgzC++pedanticbuild_x64_VS2019pedantic.vcxproj]
C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.29.30133includexstring(531): warning C4710: 'void __cdecl std::basic_ios<char,struct std::char_traits<char> >::setstate(int,bool) __ptr64': function not inlined [D:ProgrammingProgzC++pedanticbuild_x64_VS2019pedantic.vcxproj]
C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.29.30133includexstring(534): warning C4710: 'void __cdecl std::basic_ios<char,struct std::char_traits<char> >::setstate(int,bool) __ptr64': function not inlined [D:ProgrammingProgzC++pedanticbuild_x64_VS2019pedantic.vcxproj]
C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.29.30133includexstring(4475): warning C4710: 'void __cdecl std::_Xlen_string(void)': function not inlined [D:ProgrammingProgzC++pedanticbuild_x64_VS2019pedantic.vcxproj]
C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.29.30133includexmemory(229): warning C4711: function 'void * __ptr64 __cdecl std::_Allocate_manually_vector_aligned<struct std::_Default_allocate_traits>(unsigned __int64)' selected for automatic inline expansion [D:ProgrammingProgzC++pedanticbuild_x64_VS2019
pedantic.vcxproj]
C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.29.30133includexmemory(229): warning C4711: function 'void * __ptr64 __cdecl std::_Allocate_manually_vector_aligned<struct std::_Default_allocate_traits>(unsigned __int64)' selected for automatic inline expansion [D:ProgrammingProgzC++pedanticbuild_x64_VS2019
pedantic.vcxproj]
C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.29.30133includexstring(3230): warning C4710: 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > & __ptr64 __cdecl std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::assign(char const * __ptr64 c
onst,unsigned __int64) __ptr64': function not inlined [D:ProgrammingProgzC++pedanticbuild_x64_VS2019pedantic.vcxproj]
C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.29.30133includexmemory(229): warning C4711: function 'void * __ptr64 __cdecl std::_Allocate_manually_vector_aligned<struct std::_Default_allocate_traits>(unsigned __int64)' selected for automatic inline expansion [D:ProgrammingProgzC++pedanticbuild_x64_VS2019
pedantic.vcxproj]
C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.29.30133includexstring(4947): warning C4711: function 'class std::basic_ostream<char,struct std::char_traits<char> > & __ptr64 __cdecl std::operator<<<char,struct std::char_traits<char>,class std::allocator<char> >(class std::basic_ostream<char,struct std::char_tr
aits<char> > & __ptr64,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const & __ptr64)' selected for automatic inline expansion [D:ProgrammingProgzC++pedanticbuild_x64_VS2019pedantic.vcxproj]

全部来自MSVS原生标准库。我认为这种编译器行为是完全不能接受的。

如何在不更改警告级别的情况下摆脱这些警告?

MSVC启用了大量的'信息'警告,当你使用/Wall时,这些警告是'默认关闭'的。

如果你想在MSVC中使用"最大警告",那么你需要有选择地抑制一堆警告。对于我的GitHub项目,我使用这个,但有一堆抑制通常在一个头使用#pragma管理。

// Off by default warnings
#pragma warning(disable : 4619 4616 4061 4265 4365 4571 4623 4625 4626 4628 4668 4710 4711 4746 4774 4820 4987 5026 5027 5031 5032 5039 5045 5219 5246 5264 26812)
// C4619/4616 #pragma warning warnings
// C4061 enumerator 'X' in switch of enum 'X' is not explicitly handled by a case label
// C4265 class has virtual functions, but destructor is not virtual
// C4365 signed/unsigned mismatch
// C4571 behavior change
// C4623 default constructor was implicitly defined as deleted
// C4625 copy constructor was implicitly defined as deleted
// C4626 assignment operator was implicitly defined as deleted
// C4628 digraphs not supported
// C4668 not defined as a preprocessor macro
// C4710 function not inlined
// C4711 selected for automatic inline expansion
// C4746 volatile access of '<expression>' is subject to /volatile:<iso|ms> setting
// C4774 format string expected in argument 3 is not a string literal
// C4820 padding added after data member
// C4987 nonstandard extension used
// C5026 move constructor was implicitly defined as deleted
// C5027 move assignment operator was implicitly defined as deleted
// C5031/5032 push/pop mismatches in windows headers
// C5039 pointer or reference to potentially throwing function passed to extern C function under - EHc
// C5045 Spectre mitigation warning
// C5219 implicit conversion from 'int' to 'float', possible loss of data
// C5246 the initialization of a subobject should be wrapped in braces
// C5264 'const' variable is not used
// 26812: The enum type 'x' is unscoped. Prefer 'enum class' over 'enum' (Enum.3).

或者,您必须通过命令行抑制许多内容:

if( CMAKE_CXX_COMPILER_ID MATCHES "MSVC" )
set(DisableWarnings "/wd4365" "/wd4514" "/wd4625" "/wd4626" "/wd4627" "/wd4710" "/wd4820" "/wd5026" "/wd5027" "/wd5039" "/wd5045")
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.34)
list(APPEND DisableWarnings "/wd5262" "/wd5264")
endif()
target_compile_options(yourtarget PRIVATE ${DisableWarnings})

为了获得最佳的MSVC一致性,也使用/permissive- /Zc:__cplusplus /Zc:inline /Zc:preprocessor

还有一个注意事项,如果您使3.20成为最小支持的CMake,那么您可以指望在默认CL标志中没有任何/W3/GR。这样,您可以使用target_compile_options而不是和CMAKE_CXX_FLAGS玩游戏。/EHsc总是存在于MSVC-like。

相关内容

  • 没有找到相关文章

最新更新