繁体   English   中英

Visual Studio 2019 标准库生成警告

[英]Visual Studio 2019 standard library generates warnings

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

有我的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 Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\ostream(118): error C2220: the following warning is treated as an error [D:\Programming\Progz\C++\pedantic\build_x64_VS2019\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\ostream(118): warning C4710: 'void __cdecl std::basic_ostream<char,struct std::char_traits<char> >::_Osfx(void) __ptr64': function not inlined [D:\Programming\Progz\C++\pedantic\build_x64_VS2019\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\ostream(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:\Programming\Progz\C++\pedantic\buil
d_x64_VS2019\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\exception(316): warning C4710: '__cdecl std::bad_array_new_length::bad_array_new_length(void) __ptr64': function not inlined [D:\Programming\Progz\C++\pedantic\build_x64_VS2019\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xmemory(131): warning C4710: 'void __cdecl std::_Throw_bad_array_new_length(void)': function not inlined [D:\Programming\Progz\C++\pedantic\build_x64_VS2019\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xmemory(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:\Programming\Progz\C++\pedantic\build_x64_VS2019
\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xmemory(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:\Programming\Progz\C++\pedantic\build_x64_VS2019
\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xstring(511): warning C4710: 'int __cdecl std::basic_streambuf<char,struct std::char_traits<char> >::sputc(char) __ptr64': function not inlined [D:\Programming\Progz\C++\pedantic\build_x64_VS2019\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xstring(519): warning C4710: '__int64 __cdecl std::basic_streambuf<char,struct std::char_traits<char> >::sputn(char const * __ptr64,__int64) __ptr64': function not inlined [D:\Programming\Progz\C++\pedantic\build_x64_VS2019\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xstring(523): warning C4710: 'int __cdecl std::basic_streambuf<char,struct std::char_traits<char> >::sputc(char) __ptr64': function not inlined [D:\Programming\Progz\C++\pedantic\build_x64_VS2019\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xstring(531): warning C4710: 'void __cdecl std::basic_ios<char,struct std::char_traits<char> >::setstate(int,bool) __ptr64': function not inlined [D:\Programming\Progz\C++\pedantic\build_x64_VS2019\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xstring(534): warning C4710: 'void __cdecl std::basic_ios<char,struct std::char_traits<char> >::setstate(int,bool) __ptr64': function not inlined [D:\Programming\Progz\C++\pedantic\build_x64_VS2019\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xstring(4475): warning C4710: 'void __cdecl std::_Xlen_string(void)': function not inlined [D:\Programming\Progz\C++\pedantic\build_x64_VS2019\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xmemory(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:\Programming\Progz\C++\pedantic\build_x64_VS2019
\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xmemory(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:\Programming\Progz\C++\pedantic\build_x64_VS2019
\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xstring(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:\Programming\Progz\C++\pedantic\build_x64_VS2019\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xmemory(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:\Programming\Progz\C++\pedantic\build_x64_VS2019
\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xstring(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:\Programming\Progz\C++\pedantic\build_x64_VS2019\pedantic.vcxproj]

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

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

当您使用/Wall时,MSVC 会启用大量“默认情况下关闭”的“信息性”警告。 请参阅Microsoft 文档

如果你想在 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 的情况下。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM