PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label pragma. Show all posts
Showing posts with label pragma. Show all posts

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)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, April 28, 2022

[FIXED] How could I find out what kind of warnings get enabled when I use the warning-pragma?

 April 28, 2022     perl, pragma, warnings     No comments   

Issue

Is there a command or a variable that shows me all the different warnings which get enabled when I use the warning-pragma?


Solution

perldoc warnings shows the hierarchy of categories warnings uses. (Though before perl 5.20, this was in perldoc perllexwarn.)



Answered By - ysth
Answer Checked By - Mary Flores (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to disable a warning which gets re-enabled in third-party code?

 April 28, 2022     c++, pragma, suppress-warnings, visual-c++, warnings     No comments   

Issue

My project requirement is to have no warnings at all, treating them as errors, but third party tools generate their own warnings whose code I cannot access.

So I have to disable specific warnings before including the third-party headers:

#pragma warning( push )
#pragma warning( disable : 4005 ) // macro redefinition
#pragma warning( disable : 4505 ) // unreferenced local function has been removed
#include <cuda_runtime_api.h>
#pragma warning( pop )

This approach sometimes works, sometimes it does not, depending on the header file.

Could it be due to the fact that considering all the warnings as errors, #pragma warning does not apply?

Or could it be that inside the included code there are #pragmas that disable mine?

How can I suppress them?

P.S.: The program is built with the /WX (treat warnings as errors) Visual Studio flag. How can I disable it in some parts of the source code, especially for third-party code I include, with the preprocessor?


Solution

From experience with very similar issues (but with the in-built Windows 'system' headers, rather than 3rd-party stuff), I have reluctantly accepted the fact that the #pragma warning (push|pop) system doesn't really work! This seems especially true when the "Enable all warnings" (/Wall) option is set, as the #pragma warning(pop) doesn't understand what 'level number' to restore.

The only workable technique I have (so far) come up with is to explicitly disable the relevant warnings before inclusion of the '3rd-Party' headers, then (again, explicitly) reset them afterwards. Here is a short extract from the "global" header (the one I use to generate the pre-compiled header) I use for building my projects:

// Turn off warnings generated by the "standard" and "MFC" header files.  *** Note: using "#pragma(push, 2) ...
// #pragma(pop)" to embrace these header files doesn't work! ***  [For some reason (possibly something weird in
// one of the headers), warnings such as 'unused parameters' and 'signed/unsigned' are not re-enabled.] ...
#pragma warning(disable:4091)   // 'typedef' ignored on left of tagGPFIDL.
#pragma warning(disable:4191)   // unsafe conversion to AFX_PMSG(W) (MMAP)
#pragma warning(disable:4239)   // Non-standard: conv. <class> to &<class>
#pragma warning(disable:4251)   // class 'XXX' needs to have dll-interface
#pragma warning(disable:4263)   // member function does not override . ...
//... and around 30 or so other, similar lines.

#include <afxwin.h>             // Minimal set of afx... (MFC) headers for the things we want to do ... (?)
#include <afxwinappex.h>        // Required for the base application class: MFC's CWinAppEx
#include <afxmdiframewndex.h>   // Base frame windows "CMDIFrameWndEx" and "CMDIChildWndEx"
#include <mmsystem.h>           // Mulitmedia APIs: allows playing of sounds in various alert message boxes
#include <MsiQuery.h>           // Required for DLL interface to the MSI installer (itself #includes msi.h)
//... and all other warning-prone headers ...

#pragma warning(default:4091)   // 'typedef' ignored on left of tagGPFIDL.
#pragma warning(default:4191)   // unsafe conversion to AFX_PMSG(W) (MMAP)
#pragma warning(default:4239)   // Non-standard: conv. <class> to &<class>
#pragma warning(default:4251)   // class 'XXX' needs to have dll-interface
#pragma warning(default:4263)   // member function does not override . ...
//... and all the others corresponding to those that were disabled

Note that, by using the #pragma warning(default:nnnn) (rather than #pragma warning(enable:nnnn)) you are resetting the warning to the project's setting, rather than blindly enabling it.

I understand that this is rather clumsy - and, almost certainly, not what you are looking or - but it does work. Also, once you have established the basic list of warnings, it's a relatively low-maintenance solution.

PS: As far as I am aware, there is no option available to the MSVC pre-processor to either detect or change the /WX (treat warnings as errors) compiler option, although you can set this for any specific warning, with #pragma warning(error:nnnn), and 'unset' it using default.


EDIT: Another possible way of disabling the following:

warning C4505: 'bar': unreferenced local function has been removed

is to actually reference the 'offending' function. But I'm not here being sarcastic - you can include a dummy static inline function that references bar (in your header) and that will silence the warning (it isn't given for functions defined as static inline). So, assuming the function bar is defined (in the 3rd-party header) like this:

static int bar(int b)
{
    return b * b;
}

but bar is never referenced in (some of) your build units, then adding this line to your 'global' header (after the inclusion of the 3rd-party header) will kill the warning (for MSVC, but not for clang-cl):

static inline int foo(int a) { return bar(a); }

Of course, if there are many such warnings, this method could become a bit cumbersome; but again, once you have the code written, it's low-maintenance.



Answered By - Adrian Mole
Answer Checked By - Timothy Miller (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How do people handle warning C4793: 'some_function' : function compiled as native?

 April 28, 2022     c++-cli, opencv, pragma, visual-studio, warnings     No comments   

Issue

I'm using the OpenCV library and one of its header files, cxoperations.hpp, generates "warning C4793: 'anonymous namespace'::CV_XADD' : function compiled as native", if my C++ project is compiled with CLR support. I can prevent the warning by surrounding the OpenCV header include like this:

#pragma managed(push,off)
#include <cv.h>
#pragma managed(pop)

But the project that actually uses OpenCV isn't compiled with CLR support, it's a native C++ static library. The project that does have CLR support, and generates this warning without the pragma statements, simply uses this static library. So I'm a bit surprised that the warning was created at all, especially given the fact that the entire static library is not compiled with CLR support, and yet it's only this one header that causes the problem.

Thus this solution seems sub-optimal to me. Is this how you would handle this warning, or can you recommend a better practice?


Solution

I think what you want is this:

#pragma unmanaged
#include <cv.h>
#pragma managed
// managed code wrapping unmanaged opencv functions

A C++/CLI project can contain both managed and unmanaged parts, and the compiler takes care of marshalling data between the 2 for you. The managed entry points will be callable from normal .NET apps (like C# and the rest) and will use garbage collection, and they'll call unmanaged functions to do the heavy lifting.



Answered By - Blindy
Answer Checked By - Marie Seifert (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Wednesday, April 27, 2022

[FIXED] How can I disable #pragma warnings?

 April 27, 2022     c++, pragma, warnings     No comments   

Issue

While developing a C++ application, I had to use a third-party library which produced a huge amount of warnings related with a harmless #pragma directive being used.

../File.hpp:1: warning: ignoring #pragma ident
In file included from ../File2.hpp:47,
                 from ../File3.hpp:57,
                 from File4.h:49,

Is it possible to disable this kind of warnings, when using the GNU C++ compiler?


Solution

I believe you can compile with

-Wno-unknown-pragmas

to suppress these.



Answered By - Adam Wright
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

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
All Comments
Atom
All Comments

Copyright © PHPFixing