开发者

putting #warning inside #define's body

In a header that should be able to compile in C and C++ files, in Visual Studio (2010) and MinGW (32-bit - v3.4.5, 64-bit - v4.5.0) I am trying to minimize the size by changing each one of such line (there are many of them):

// for symbol A
#ifdef __GNUC__
# warning Symb开发者_如何学编程ol A is deprecated. Use predefined const cnA instead.
#else
# pragma message("Symbol A is deprecated. Use predefined const cnA instead.")
#endif

// Same for B
// Same for C
// . . . 

to

// define this once:
#ifdef __GNUC__
# define X_Warning(x) #warning "Symbol " x " is deprecated. Use cn" x  // (1)
#else
# define X_Warning(x) __pragma(message("Symbol " x " is deprecated. Use cn" x "))
#endif

// and use like this:
X_Warning("A")
X_Warning("B")
X_Warning("C")

or, at least to this:

// define this once:
#ifdef __GNUC__
# define Y_Warning(x) #warning x   // (2)
#else
# define Y_Warning(x) __pragma(message(x))
#endif

// and use like this:
Y_Warning("Symbol A is deprecated. Use predefined const cnA instead.")
Y_Warning("Symbol B is deprecated. Use predefined const cnB instead.")
Y_Warning("Symbol C is deprecated. Use predefined const cnC instead.")
. . .

But the line marked with (1) doesn't work.

__pragma is Microsoft's equivalent for #pragma to use in this kind of situations.

  1. What is the right way to do this?
  2. Is it even possible for MinGW/gcc?
  3. is __GNU__ right symbol to use for such things?

P.S. I forgot to mention that A, B, C.. are #define-ed symbols. In that case it is not possible to do with my old MinGW v3.4.5 (at least in my case with this particular configuration). And @Edwin's answer is correct.

But _Pragma is supported by newer versions of MingW and, thanks to @Christoph for an answer, it is possible to do as follows:

// define this once:
#ifdef __GNUC__
# define DO_PRAGMA(x) _Pragma (#x)
# define X_Warning(x) DO_PRAGMA( message "Symbol " #x " is deprecated. Use cn" x )
#else
# define X_Warning(x) __pragma(message("Symbol " x " is depricated. Use cn" x ))
#endif

// and use like this:
#ifdef A
  X_Warning("A")
#endif
#ifdef C
  X_Warning("B")
#endif
#ifdef B
  X_Warning("C")
#endif

Marking as deprecated seems to work in some cases, but not for me. It requires you to define deprecated symbols before their usage which is not my case and beyond my control.


In msvs 2010 there is this :

 #pragma deprecated( identifier1 [,identifier2, ...] )

don't know about other compilers

and microsoft specific:

__declspec(deprecated) void func1(int) {}
__declspec(deprecated("** this is a deprecated function **")) void func2(int) {}
__declspec(deprecated(MY_TEXT)) void func3(int) {}


GCC supports the C99 _Pragma operator and even comes with example code for your use case.


You can not construct #pragma or #warning with macro but i think you can do something like:

#ifdef __GNUC__
# define Y_Warning(x) message x
#else
# define Y_Warning(x) message(x)
#endif

#pragma Y_Warning("Symbol A is depreboned. Use predefined const cnA instead.")
#pragma Y_Warning("Symbol B is depronated. Use predefined const cnB instead.")
#pragma Y_Warning("Symbol C is deprecuted. Use predefined const cnC instead.")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜