如何检查__builtin_函数在gcc上可用 [英] How to check __builtin_ function is available on gcc
问题描述
我需要知道gcc是否有一种方法可以检查那些很棒的 __ builtin_MY_DESIRED_FUNCTIONs
I need to know is there a method for gcc to check presence of those awesome __builtin_MY_DESIRED_FUNCTIONs
例如,我想使用 __ builtin_nan
并确保它可用于我的程序,并且在编译期间不会失败.
For example, I'd like to use __builtin_nan
and be sure it is available for my program and it won't fail during compilation time.
我会更具体:在clang上有 __ has_builtin
"checker",这样我们就可以像
I'll be more specific: on clang there is __has_builtin
"checker" so we can write smth like
#if __has_builtin(__builtin_nan)
但是我找不到gcc的类似物.
But I can't find analog for gcc.
也许我可以仅仅依靠gcc,例如哦,我现在在gcc上,让我们假设所有这些 __ builtin _
都在下面的示例中……"
And probably I can rely just on gcc, like "Oh, I'm on gcc now, just let's assume all of those __builtin_
are here like in example below..."
#if __GNUC__
double mynan = __builtin_nan("0");
#endif
在有人放置此"-fno-builtin"编译标志之前,它可能会起作用.
And probably it will work, till someone put this "-fno-builtin" compilation flag.
推荐答案
否,您将不得不使用 __ GNUC __
和 __ GNUC_MINOR __
(和 __ GNUC_PATCHLEVEL __
>如果您使用的是此类gcc版本),请测试每个版本特定的内置功能(可以在此处)
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)
例如:
/* __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
这篇关于如何检查__builtin_函数在gcc上可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!