对于以下测试代码,预期的结果是由于 #error
指令而无法编译。
#if __has_builtin(__builtin_types_compatible_p)
#error ("Hello")
#endif
#include <stdio.h>
int main (void)
{
if (__builtin_types_compatible_p(int, const int)) {
printf("INT ARE SAME!!!\n");
}
return 0;
}
然而,它不仅编译而且还打印出 INT ARE SAME!!!
这意味着即使 __builtin_types_compatible_p
存在,__has_builtin
没有检测到它。为什么?更重要的是,是否有其他方法可以检测可用的内置函数?
这是在 Clang 3.8 和 3.9(当前的稳定分支)上测试的。代码是使用 clang --std=c99 -Wall -Wextra test.c
编译的。
最佳答案
注意:自 Clang 10 起,此答案已过时。
根据一位 LLVM 开发人员的说法,
__has_builtin detects builtin functions.
在您的示例中,__builtin_types_compatible_p 是
not a function at all, since it takes a type, not a value.
因此,__has_builtin(__builtin_types_compatible_p)
返回 false,这与设计完全一样。
http://lists.llvm.org/pipermail/cfe-dev/2017-July/054590.html
关于Clang 的 __has_builtin 并不总是有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42744425/