gpt4 book ai didi

c - _Static_assert 与 __has_include

转载 作者:行者123 更新时间:2023-12-02 04:36:42 27 4
gpt4 key购买 nike

我想制作宏,检查文件是否“可包含”。像这样:

#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

这可能吗?或者可能有不同的解决方案会产生类似的结果?

感谢您的帮助!

最佳答案

如果一切都失败了(你很难反驳:“__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

关于c - _Static_assert 与 __has_include,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41855464/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com