- What should I do when I want to compile with Werrors if the code in the Header-only library contains Deprecated code?

Asked 2 years ago, Updated 2 years ago, 31 views

-Werror compiles a project and I don't want to suppress the entire error because of the Deprecated HeaderOnlyLibrary, but how do I suppress this library only? Is there a common method for gcc, clang, msvc?

Example:

deprecated.hpp

 volatile intb=0;

voida(){
    b + = 1;
}

main.cpp

#include "deprecated.hpp"

int main() {
    a();
    return 0;
}

Compilation error indication:

$g++main.cc-Wall-Wextra-Werror-std=gnu++2b

deprecated.hpp:In function 'void a()':
deprecated.hpp:4:6:error:compound assignment with 'volatile'-qualified left operand is deprecated [-Werror=volatile]
    4 | b + = 1;
      |     ~^~~
cc1plus —all warnings being created as errors

c++

2022-09-30 17:04

2 Answers

I didn't really understand the intent of Deprecated, but if deprecated.hpp is the system header, I think it's better to write #include<deprecated.hpp> and if not, correct deprecated.hpp.

The compiler makes a clear distinction between the system header and the normal header.The former is #include<filename>, while the latter is #include "filename".
In addition, the GCC suppresses the warning if it is a system header (you can enable the warning in -Wsystem-headers).MSVCs can also suppress alerts to system headers with /external:anglebrackets.


2022-09-30 17:04

There is a way to temporarily disable alerts in the preprocessor directive.

//Clang also defines _GNUC__.
# if defined (_clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wdeprecated-volatile"
# elif defined (__GNUC__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored " - Wvolatile"
# elif defined(_MSC_VER)
# pragma warning (push)
//# pragma warning (disable:xxxx)
#endif

# include "hoge.hpp"

# if defined (_clang__)
# pragma clang diagnostic pop
# elif defined (__GNUC__)
# pragma GCC diagnostic pop
# elif defined(_MSC_VER)
# pragma warning (pop)
#endif

In addition, GCC/Clang uses -Wno-error to always treat -Werror as a warning instead of an error.

< u l >
  • Clang:-Wno-error=deprecated-volatile
  • GCC:-Wno-error=volatile
  • It is not clear if there is a way to partially negate the /WX on the MSVC to alert only certain numbers.

    Refer to the following references for MSVCs:

    < u l >
  • warning pragma|Microsoft Docs
  • /w, /W0, /W1, /W2, /W3, /W4, /w1, /w3, /w4, /Wall, /wd, /we, /wo, /wv, /WX (Warning level)|Microsoft Docs
  • In any case, we strongly recommend that you correct the user code without ignoring the warning.


    2022-09-30 17:04

    If you have any answers or tips


    © 2024 OneMinuteCode. All rights reserved.