我想制作宏,检查该文件是否“可包含”。 像这样:
#define CHECK(__file) \
_Static_assert( __has_include(__file), "Error")
CHECK("header.h");
但它会产生错误,如
f.c:28:3: __has_include must be used within a preprocessing directive
f.c:28:3: Implicit declaration of function '__has_include' is invalid in C99
f.c:28:3: Static_assert expression is not an integral constant expression
这甚至可能吗?或者也许有不同的解决方案会产生类似的结果?
感谢您的帮助!
答案 0 :(得分:0)
如果全部失败(并且您很难争辩:“__ has_include必须在预处理指令中使用”),则在其上抛出另一层间接:
h_assert(){
for h; do
printf '
#if !__has_include("%s")
_Static_assert(0,"__has_include(\"%s\")");
#endif
' "$h" "$h"
done
}
h_assert foo.h bar.h baz.h #Generate the header assertions with a POSIX shell