How to ignore unrecognized GCC pragma warning of CUDA

Problem

You have a GCC pragma in your CUDA code to ignore a specific warning produced by your code. While the GCC compiler uses this for the C++ section of the code, the CUDA compiler frontend complains about it with a warning:

foo.cu(44): warning: unrecognized GCC pragma

Solution

To request the frontend of the CUDA compiler to ignore or hide this warning, pass this option during compilation:

-Xcudafe "--diag_suppress=unrecognized_gcc_pragma"

The GCC pragma warning should be hidden now.

Tried with: CUDA 5.5, GCC 4.8 and Ubuntu 12.04

How to selectively ignore a GCC warning

You may sometimes want to ignore or disable a specific type of warning that is generated when compiling your C or C++ code. The diagnostic pragmas of GCC and G++ compilers can be used to achieve this.

  • For example, to ignore the warning of type -Wenum-compare for an entire C++ source file, add this to your code at the top:
#pragma GCC diagnostic ignored "-Wenum-compare"
/**
 * Code that generates this warning
 */
  • If you want the warning to be ignored only for a certain section of the code, surround it with push and pop directives:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wenum-compare"
/**
 * Code that generates this warning
 */
#pragma GCC diagnostic pop

Note: As of GCC 5.1, few of the warnings in G++ (that is, only for C++ code) will not be ignored even when this pragma is used! More details here and here.

Tried with: GCC 4.8 and Ubuntu 12.04 LTS

Visual C++: #pragma message

What if a programmer wants to draw attention to certain sections of code in certain source files? This could be for oneself or other team members. It could be certain work-in-progress, debugging or warning messages to watch out for. This could be for today or when one comes back to this code a long time later in the future.

One elegant and convenient way to achieve this is by using #pragma message. When the compiler finds a #pragma message, it outputs the message string to the Output window of Visual Studio. #pragma message strings can be left at the sections of code one wants to be signaled about. This way, even if a programmer comes back to some very old code, as soon as he compiles it the compiler signals him about certain information to watch out for in the code.

Using #pragma message is easy, like this:

// Foo.cpp
// ... Lots of Foo code ...

#pragma message( "HACK: Count of foo items updated! REMOVE later!" )
extern int debugCount;
++debugCount;

// ... Lots of Foo code ...

#pragma message can be tweaked to also output other information like the filename, line number and timestamp. Please see the MSDN article on #pragma message for more on that.

Note: The #pragma message output by the compiler can easily get lost in the Output window if the code produces lots of other warnings and errors. So, this is yet another reason why it is a good habit to make sure the code compiles with ZERO warnings and errors! 😉

Visual C++: pragma warning

It is a good practice to compile code at the highest warning level available from the compiler. When the compiler reports warnings on the code, the code should be fixed so that it compiles with zero warnings. But, sometimes this is not possible, for example, if the source of the warnings are headers included from external libraries. Common C++ libraries like Boost and special libraries like CGAL (Computational Geometry Algorithms Library) are notorious for introducing hundreds of warnings when their header files are included.

In Visual C++, the #pragma warning compiler directive is an effective way to disable these specific warnings. There are many specifiers that can be specified with #pragma warning. Here are a couple that I find most useful:

Warnings that are reported have an identifier and a textual message. The identifier is of the form CXXXX, where XXXX is the warning number. The disable specifier can be used to disable a specific warning, say C1234:

#pragma warning(disable: 1234)

The disabling of warnings should be localized as much as possible. This can be done using the push and pop specifiers around the offending code.

#pragma warning(push)
#pragma warning(disable: 1234)
// Offending code that produces C1234 warning goes here
#pragma warning(pop)

#pragma warning directives can be placed in header files or in source files. When the directive needs to be used across an entire source file or many files, one can get lazy and place it in a header that is included in all these files. This is not a good practice since the warning will be disabled for all the source files that include that header now or in the future.

I prefer placing directives at the source file level, so that their effect is localized to the source file. If one is placing the directive at the source file, it should be put at the top, even above the header files inclusion, to be effective:

// Foo.cpp
#pragma warning(disable: 1234)
#include "Foo.h"

Foo::Foo()
{ /***/ }

Visual C++: Library Pragma

I find myself having to indicate the libraries I want linked in every time I do this. I found a neat (non-portable) trick in Visual Studio to do this.

Use the #pragma comment(lib, "libfile") [1] preprocessor directive to hint your compiler/linker to include these library files for linking. For example:

// Link cg libraries
#pragma comment(lib, "cg.lib")
#pragma comment(lib, "cggl.lib")

[1] msdn.microsoft.com/library/en-us/vclang/html/_predir_comment.asp

(via Adding MSDEV Libraries)