[英]Is it possible to print a preprocessor variable in C?
Is is possible to print to stderr the value of a preprocessor variable in C ? 是否可以在C中打印到stderr预处理器变量的值? For example, what I have right now is:
例如,我现在拥有的是:
#define PP_VAR (10)
#if (PP_VAR > 10)
#warning PP_VAR is greater than 10
#endif
But what I'd like to do is: 但我想做的是:
#define PP_VAR (10)
#if (PP_VAR > 10)
#warning PP_VAR=%PP_VAR%
#endif
Is something like this possible in C ? 在C中这样的事情可能吗?
You can print out the value of a preprocessor variable under visual studio. 您可以在visual studio下打印出预处理器变量的值。 The following prints out the value of _MSC_VER:
以下打印出_MSC_VER的值:
#define STRING2(x) #x
#define STRING(x) STRING2(x)
#pragma message(STRING(_MSC_VER))
Not sure how standard this is though. 不确定这是多么标准。
This works with GCC 4.4.3: 这适用于GCC 4.4.3:
#define STRING2(x) #x
#define STRING(x) STRING2(x)
#pragma message "LIBMEMCACHED_VERSION_HEX = " STRING(LIBMEMCACHED_VERSION_HEX)
yields: 收益率:
src/_pylibmcmodule.c:1843: note: #pragma message: LIBMEMCACHED_VERSION_HEX = 0x01000017
Many C compilers support #warning
(but it is not defined by the C standard). 许多C编译器支持
#warning
(但它不是由C标准定义的)。
However, GCC at least does not do pre-processing on the data that follows, which means it is hard to see the value of a variable. 但是,GCC至少不对后面的数据进行预处理,这意味着很难看到变量的值。
#define PP_VAR 123
#warning "Value of PP_VAR = " PP_VAR
#warning "Value of PP_VAR = " #PP_VAR
#warning "Value of PP_VAR = " ##PP_VAR
GCC produces: GCC生产:
x.c:2:2: warning: #warning "Value of PP_VAR = " PP_VAR
x.c:3:2: warning: #warning "Value of PP_VAR = " #PP_VAR
x.c:4:2: warning: #warning "Value of PP_VAR = " ##PP_VAR
Use the preprocessor token-pasting operator: ##TOKEN_NAME 使用预处理程序令牌粘贴操作符:## TOKEN_NAME
As previously noted, the preprocessor directives you are using are non-standard, so YMMV. 如前所述,您使用的预处理程序指令是非标准的,因此YMMV。
Well, what you are doing is actually non-standard. 那么,你在做什么实际上是非标准的。 Firstly, the "#warning" or "#warn" directive is not standard.
首先,“#warning”或“#warn”指令不是标准的。 Secondly, when using the preprocessor, the line must begin with the pound symbol, without any spaces:
其次,在使用预处理器时,该行必须以井号开头,不带任何空格:
#ifdef BLAH1 # define BLAH2 // OK, because pound is at the very left. #endif #ifdef BLAH3 #define BLAH4 // Works on many compilers, but is non-standard. #endif
Since you are already using a non-standard extension, you will need to look up the documentation of the particular preprocessor/compiler that you are using to see what it says about "#warning". 由于您已经在使用非标准扩展,因此您需要查找正在使用的特定预处理器/编译器的文档,以查看它所说的“#warning”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.