把#warning放在#define的体内

sny*_*sny 6 c c++ mingw visual-studio-2010

在一个应该能够在C和C++文件中编译的头文件中,在Visual Studio(2010)和MinGW(32位 - v3.4.5,64位 - v4.5.0)中,我试图通过更改每个文件来最小化其中一条线(有许多线):

// for symbol A
#ifdef __GNUC__
# warning Symbol 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
// . . . 
Run Code Online (Sandbox Code Playgroud)

// 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")
Run Code Online (Sandbox Code Playgroud)

或者,至少对此:

// 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.")
. . .
Run Code Online (Sandbox Code Playgroud)

但标有(1)的行不起作用.

__pragma微软相当于#pragma在这种情况下使用.

  1. 这样做的正确方法是什么?
  2. MinGW/gcc甚至可能吗?
  3. __GNU__用这种东西的正确符号吗?

PS我忘了提到A,B,C ..是#defined-ed符号.在这种情况下,不可能使用我的旧MinGW v3.4.5(至少在我的情况下使用此特定配置).而@ Edwin的回答是正确的.

但_Pragma受到更新版MingW的支持,感谢@Christoph的回答,可以做到如下:

// 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
Run Code Online (Sandbox Code Playgroud)

标记为已弃用似乎在某些情况下有效,但不适用于我.它要求您在使用之前定义已弃用的符号,这不是我的情况,也不在我的控制之下.

eng*_*010 4

在msvs 2010中有这样的:

 #pragma deprecated( identifier1 [,identifier2, ...] )
Run Code Online (Sandbox Code Playgroud)

不知道其他编译器

和微软特定的:

__declspec(deprecated) void func1(int) {}
__declspec(deprecated("** this is a deprecated function **")) void func2(int) {}
__declspec(deprecated(MY_TEXT)) void func3(int) {}
Run Code Online (Sandbox Code Playgroud)

  • GCC 等效项是 `__attribute__((deprecated))`,请参阅 http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html 。 (2认同)