首页 > 解决方案 > 如何检查 __builtin_ 函数在 gcc 上是否可用

问题描述

我需要知道 gcc 是否有一种方法来检查那些真棒的存在__builtin_MY_DESIRED_FUNCTIONs

例如,我想使用__builtin_nan并确保它可用于我的程序,并且在编译期间不会失败。

我会更具体地说:在clang上有__has_builtin“检查器”,所以我们可以这样写

#if __has_builtin(__builtin_nan)

但我找不到 gcc 的模拟。

也许我可以只依赖 gcc,比如“哦,我现在使用 gcc,让我们假设所有这些__builtin_都在这里,如下面的示例......”

#if __GNUC__
double mynan = __builtin_nan("0");
#endif

也许它会起作用,直到有人把这个“-fno-builtin”编译标志。

标签: cgccmacrosbuilt-in

解决方案


No, you will have to use __GNUC__ and __GNUC_MINOR__ (and __GNUC_PATCHLEVEL__ if you use such gcc versions) to test for each release specific builtin function (gcc releases can be found here)

For example:

/* __builtin_mul_overflow_p added in gcc 7.4 */
#if (__GNUC__ > 7) || \
         ((__GNUC__ == 7) && (__GNUC_MINOR__ > 3))
#define BUILTIN_MUL_OVERFLOW_EXIST
#endif

#ifdef BUILTIN_MUL_OVERFLOW_EXIST
int c = __builtin_mul_overflow_p (3, 2, 3) ? 0 : 3 * 2;
#endif

And there is an open bug for exactly what you are asking about, in here.


推荐阅读