首页 > 解决方案 > 如何删除特定的警告 gcc

问题描述

我有一个特定的警告,我想删除/隐藏然后在之后启用它。您知道如何在不删除所有其他警告的情况下做到这一点吗?

这是我收到的警告

warning: enumeration value 'EtatCCSortie' not handled in switch [-Wswitch]

而且我不需要在交换机中实际使用该状态(它是一个状态机)。我有办法不再收到此警告,即通过将状态添加到开关中,如下所示:

while (etatsImprimanteCasImpressionPeriodique != EtatIPSortie)
{
         switch (etatsImprimanteCasChangementCartouche)
         {
               ....
               case EtatCCSortie:
               break;

但我不必添加它,因为它永远不会执行(而!=)。这就是为什么我正在寻找一种方法来禁用此特定警告,然后在此切换后启用它。

Ps:我知道警告的重要性,我只需要一种方法来删除它。

标签: cgccgcc-warning

解决方案


使用诊断编译指示

enum somenum {
    EtatIPSortie,
    EtatCCSortie,
};
int etatsImprimanteCasImpressionPeriodique;
enum somenum etatsImprimanteCasChangementCartouche;

void func() {
    while (etatsImprimanteCasImpressionPeriodique != EtatIPSortie)
    {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch"
            switch (etatsImprimanteCasChangementCartouche)
            {
                case EtatCCSortie:
                break;
            }
#pragma GCC diagnostic pop
    }
}

推荐阅读