PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, June 26, 2022

[FIXED] How to treat specific warnings as errors in c++ to be cross-platform?

 June 26, 2022     c++, compiler-errors, compiler-warnings, pragma, preprocessor     No comments   

Issue

I need to treat some specific warnings as errors to ensure the program runs as it is supposed to. For instance functions with [[nodiscard]] attribute should always return, otherwise the compiler prints an error. In Visual Studio (MSVC), it is easy to do that with:

#pragma warning (error: warning_id)

This works perfectly. But I run this code on a cluster, where I use either GCC, Clang or the Intel compiler, so I would like to implement this to be portable. Something like:

#if defined(_MSC_VER)
    #pragma warning (error: 4834)
#elif defined(__GNUC__)
    // what here?
#elif defined(__clang__)
    // what to put here?
#else
    // another compiler...
#endif

I suppose Intel is similar to MSVC; in Clang, there is an option to treat an error as warning -Wno-error=some_error, which would help me the other way around but there may be too many warnings, which I would rather not treat as errors.

Can anybody help with this?


Solution

For GCC and clang, the #pragma to elevate a specific warning to an error is very similar.

For GCC:

#pragma GCC diagnostic error "-Wunused-result"

For clang:

#pragma clang diagnostic error "-Wunused-result"

The Intel C/C++ compiler does, as you presume, support the MSVC-style #pragma (and it also defines the _MSC_VER macro, so you can use the same #if defined... block).

For "other" compilers, it's clearly very difficult to say – you would need to check the manual for each compiler you are likely to use. As far as I know, there is no standard (cross-platform) way to do this. Also note that, just because a compiler pre-defines the _MSC_VER macro, does not guarantee that it will also support all MSVC-style #pragma syntax.



Answered By - Adrian Mole
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing