如何检查 __builtin_ 函数在 gcc 上是否可用
How to check __builtin_ function is available on gcc
我想知道 gcc 是否有一种方法来检查那些很棒的东西的存在 __builtin_MY_DESIRED_FUNCTIONs
例如,我想使用 __builtin_nan
并确保它可用于我的程序并且在编译期间不会失败。
我会更具体一点:在 clang 上有 __has_builtin
"checker" 所以我们可以写成
#if __has_builtin(__builtin_nan)
但是我找不到 gcc 的模拟。
也许我可以只依赖 gcc,比如 "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”编译标志。
不,您必须使用 __GNUC__
和 __GNUC_MINOR__
(如果您使用此类 gcc 版本,则 __GNUC_PATCHLEVEL__
)来测试每个版本特定的内置函数(gcc 版本可以是找到 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
here.
中有一个针对您所询问的问题的未解决错误
好消息!一个 __has_builtin
was added in GCC 10 (see change notes):
The special operator __has_builtin (operand) may be used in constant
integer contexts and in preprocessor ‘#if’ and ‘#elif’ expressions to
test whether the symbol named by its operand is recognized as a
built-in function by GCC in the current language and conformance mode.
It evaluates to a constant integer with a nonzero value if the
argument refers to such a function, and to zero otherwise. The
operator may also be used in preprocessor ‘#if’ and ‘#elif’
expressions. The __has_builtin operator by itself, without any operand
or parentheses, acts as a predefined macro so that support for it can
be tested in portable code. Thus, the recommended use of the operator
is as follows:
#if defined __has_builtin
# if __has_builtin (__builtin_object_size)
# define builtin_object_size(ptr) __builtin_object_size (ptr, 2)
# endif
#endif
#ifndef builtin_object_size
# define builtin_object_size(ptr) ((size_t)-1)
#endif
我想知道 gcc 是否有一种方法来检查那些很棒的东西的存在 __builtin_MY_DESIRED_FUNCTIONs
例如,我想使用 __builtin_nan
并确保它可用于我的程序并且在编译期间不会失败。
我会更具体一点:在 clang 上有 __has_builtin
"checker" 所以我们可以写成
#if __has_builtin(__builtin_nan)
但是我找不到 gcc 的模拟。
也许我可以只依赖 gcc,比如 "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”编译标志。
不,您必须使用 __GNUC__
和 __GNUC_MINOR__
(如果您使用此类 gcc 版本,则 __GNUC_PATCHLEVEL__
)来测试每个版本特定的内置函数(gcc 版本可以是找到 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
here.
中有一个针对您所询问的问题的未解决错误好消息!一个 __has_builtin
was added in GCC 10 (see change notes):
The special operator __has_builtin (operand) may be used in constant integer contexts and in preprocessor ‘#if’ and ‘#elif’ expressions to test whether the symbol named by its operand is recognized as a built-in function by GCC in the current language and conformance mode. It evaluates to a constant integer with a nonzero value if the argument refers to such a function, and to zero otherwise. The operator may also be used in preprocessor ‘#if’ and ‘#elif’ expressions. The __has_builtin operator by itself, without any operand or parentheses, acts as a predefined macro so that support for it can be tested in portable code. Thus, the recommended use of the operator is as follows:
#if defined __has_builtin
# if __has_builtin (__builtin_object_size)
# define builtin_object_size(ptr) __builtin_object_size (ptr, 2)
# endif
#endif
#ifndef builtin_object_size
# define builtin_object_size(ptr) ((size_t)-1)
#endif