C 预处理器 warning/error 消息中的字符串插值
String interpolation in C preprocessor warning/error messages
我正在对一些需要特定版本 OpenSSL 的代码进行故障排除。如果 OpenSSL 导出的版本号不够高,则会返回警告,并关闭程序的各个部分。
代码如下所示:
#if OPENSSL_VERSION_NUMBER >= 0x10002000
//code here
#else
#warning "http_auth_ldap: Compiling with OpenSSL < 1.0.2, certificate verification will be unavailable"
现在,为了用户的利益,我想在此消息中包含报告的版本号。
docs for CPP 表示:
Neither #error
nor #warning
macro-expands its argument. Internal whitespace sequences are each replaced with a single space. The line must consist of complete tokens. It is wisest to make the argument of these directives be a single string constant; this avoids problems with apostrophes and the like.
这似乎使我无法将 #OPENSSL_VERISON_NUMBER
粘贴到邮件末尾。
这段代码的作者尝试了字符串化方法detailed in this question,但似乎不起作用:
#// used for manual warnings
#define XSTR(x) STR(x)
#define STR(x) #x
这会导致警告读数:
warning: http_auth_ldap: Compiling with OpenSSL < 1.0.2, certificate verification will be unavailable. OPENSSL_VERSION_NUMBER == OPENSSL_VERSION_NUMBER [-W#pragma-messages]
..和构建失败。 #pragma message
似乎受到与 #warning
.
相同的无宏扩展限制
是否有合理的方法将版本字符串连接到错误中?
由于 #warrning
不可移植,您可以尝试利用您了解的关于您的实现的其他信息。
#define cat(a,b) cat2(a,b)
#define cat2(a,b) a##b
#define FOO 199
#define REQUIRED_FOO 200
#if FOO < REQUIRED_FOO
void cat(cat(FOO_is_, FOO),
cat(_required_FOO_is_, REQUIRED_FOO))()
{
#warning Busted!
}
#endif
Demo.
这里我利用 gcc 打印发生错误或警告的函数的名称(在宏展开后!)这一事实。
我正在对一些需要特定版本 OpenSSL 的代码进行故障排除。如果 OpenSSL 导出的版本号不够高,则会返回警告,并关闭程序的各个部分。
代码如下所示:
#if OPENSSL_VERSION_NUMBER >= 0x10002000
//code here
#else
#warning "http_auth_ldap: Compiling with OpenSSL < 1.0.2, certificate verification will be unavailable"
现在,为了用户的利益,我想在此消息中包含报告的版本号。
docs for CPP 表示:
Neither
#error
nor#warning
macro-expands its argument. Internal whitespace sequences are each replaced with a single space. The line must consist of complete tokens. It is wisest to make the argument of these directives be a single string constant; this avoids problems with apostrophes and the like.
这似乎使我无法将 #OPENSSL_VERISON_NUMBER
粘贴到邮件末尾。
这段代码的作者尝试了字符串化方法detailed in this question,但似乎不起作用:
#// used for manual warnings
#define XSTR(x) STR(x)
#define STR(x) #x
这会导致警告读数:
warning: http_auth_ldap: Compiling with OpenSSL < 1.0.2, certificate verification will be unavailable. OPENSSL_VERSION_NUMBER == OPENSSL_VERSION_NUMBER [-W#pragma-messages]
..和构建失败。 #pragma message
似乎受到与 #warning
.
是否有合理的方法将版本字符串连接到错误中?
由于 #warrning
不可移植,您可以尝试利用您了解的关于您的实现的其他信息。
#define cat(a,b) cat2(a,b)
#define cat2(a,b) a##b
#define FOO 199
#define REQUIRED_FOO 200
#if FOO < REQUIRED_FOO
void cat(cat(FOO_is_, FOO),
cat(_required_FOO_is_, REQUIRED_FOO))()
{
#warning Busted!
}
#endif
Demo.
这里我利用 gcc 打印发生错误或警告的函数的名称(在宏展开后!)这一事实。