代码之家  ›  专栏  ›  技术社区  ›  virgesmith

当警告是错误时使用[[deprecated]]属性(-Werror)

  •  1
  • virgesmith  · 技术社区  · 6 年前

    [[deprecated]] 功能,例如。

    #include <string>
    #include <iostream>
    
    [[deprecated]]
    int f() { return 42; }
    
    int main()
    {
      std::cout << f() << std::endl;
    }
    

    用编译

    g++ example.cpp -std=c++14 -Werror
    

    显然是用 #pragma 对警告置之不理完全没有意义。有没有办法告诉g++发出警告,但不把特定的警告视为错误?

    2 回复  |  直到 6 年前
        1
  •  10
  •   derekerdmann    3 年前

    你需要加上

    -Wno-error=deprecated-declarations
    

    告诉gcc deprecated-declarations 作为一个警告而不是一个错误。

    您可以添加其他

    -Wno-error=name_of_warning
    

    如果您有额外的警告,您不想被视为错误以及。

        2
  •  3
  •   Some programmer dude    5 年前

    使用 -Wno-error= 后跟警告名称(与警告或错误一起显示)将禁用该特定项的错误。

    为了你的案子 [[deprecated]] ,使用选项 -Wno-error=deprecated-declaration