【问题标题】:Disable Warnings from Google Test禁用来自 Google 测试的警告
【发布时间】:2021-06-30 10:55:51
【问题描述】:

我刚刚安装了新的 Visual Studio 16.10.3 并将 Google Test 添加到我的命令控制台应用程序中。蝙蝠右侧,我收到以下警告:

Severity Code Description Project File Line Suppression State
Warning C26812 The enum type 'testing::TestPartResult::Type' is unscoped. Prefer 'enum class' over 'enum' (Enum.3). Tests D:\C++\ConsoleTest\packages\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.1.8.1.4\build\native\include\gtest\gtest-test-part.h 62
Warning C26495 Variable 'testing::internal::Mutex::critical_section_' is uninitialized. Always initialize a member variable (type.6). Tests D:\C++\ConsoleTest\packages\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.1.8.1.4\build\native\include\gtest\internal\gtest-port.h 1804
Warning C26495 Variable 'testing::internal::Mutex::critical_section_init_phase_' is uninitialized. Always initialize a member variable (type.6). Tests D:\C++\ConsoleTest\packages\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.1.8.1.4\build\native\include\gtest\internal\gtest-port.h 1804
Warning C26495 Variable 'testing::internal::Mutex::owner_thread_id_' is uninitialized. Always initialize a member variable (type.6). Tests D:\C++\ConsoleTest\packages\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.1.8.1.4\build\native\include\gtest\internal\gtest-port.h 1804
Warning C26495 Variable 'testing::internal::Mutex::type_' is uninitialized. Always initialize a member variable (type.6). Tests D:\C++\ConsoleTest\packages\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.1.8.1.4\build\native\include\gtest\internal\gtest-port.h 1804
Warning C26812 The enum type 'testing::internal::Mutex::StaticConstructorSelector' is unscoped. Prefer 'enum class' over 'enum' (Enum.3). Tests D:\C++\ConsoleTest\packages\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.1.8.1.4\build\native\include\gtest\internal\gtest-port.h 1804

我尝试通过包围 #include "pch.h" as described here 来抑制警告:

#pragma warning(push, 0)  
#include "pch.h"
#pragma warning(pop)

但是,这不起作用 - 相反,我只收到了一个额外的警告

#pragma warning(pop): 没有匹配的 '#pragma warning(push)' (C4193)

我还阅读了Broken Warnings Theory。不幸的是,我不得不承认我不明白。我认为这是相关部分:

要抑制外部标头中的警告,我们需要同时指定哪些标头是外部标头以及这些标头中的警告级别应该是什么:

cl.exe /experimental:external /external:I some_lib_dir /external:W0 /W4 my_prog.cpp

这将有效地消除 some_hdr.hpp 中的任何警告,同时保留 my_prog.cpp 中的警告。

但是我应该在哪里添加这些开关以及 googletest 的正确路径变量是什么?

提前感谢您的帮助!

【问题讨论】:

  • 您必须在此处指定要禁止显示的警告编号:#pragma warning(push, 26812) 并直接在 pch 文件中执行此操作
  • 也许,作为另一种选择,您可以单独编译您的测试文件(没有警告)并将所有内容链接在一起?
  • 使用 gcc,您可以通过 -isystem 将它们作为系统标头包含在内,以消除警告,但不确定 VS 等价物是什么
  • @RoQuOTriX:很遗憾,这不起作用,尽管我之前说过,将其移至“pch.h”也没有任何效果。
  • @Yksisarvinen:我不知道该怎么做。

标签: c++ visual-studio googletest


【解决方案1】:

代替

通过包围#include "pch.h"来抑制警告

您应该使用 warning pragma 来包围两个头文件(gtest-port.hgtest-test-part.h),可以在 Visual Studio 项目的 Solution Explorer 中的 External Dependencies 中找到它们。具体方法如下。

  • 将以下代码添加到gtest-port.h的开头和结尾

    #pragma warning( push )
    #pragma warning( disable : 26495 )
    #pragma warning( disable : 26812 )
    
    // Original Code
    
    #pragma warning( pop )
    
  • 同样,在gtest-test-part.h的开头和结尾添加如下代码

    #pragma warning( push )
    #pragma warning( disable : 26812 )
    
    // Original Code
    
    #pragma warning( pop )
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 2018-01-21
    • 2015-12-22
    • 1970-01-01
    • 2022-01-24
    相关资源
    最近更新 更多