我如何在不使用 AUTOCONF 的情况下准确地找出 C 编译器支持的说明符(如果有):内联、__inline__ 或 __inline?

How can I find out without using AUTOCONF exactly what specifier (if any) is supported by a C compiler: inline, __inline__ or __inline?

我们的项目使用了很多小的单行内联函数来进行简单的算术计算。我们如何才能准确地找出用户的 C 编译器使用的内联说明符:inline__inline__inline__

我们研究了 GLIB 库是如何做到的。片段如下:

/* inlining hassle. for compilers that don't allow the `inline' keyword,
 * mostly because of strict ANSI C compliance or dumbness, we try to fall
 * back to either `__inline__' or `__inline'.
 * we define G_CAN_INLINE, if the compiler seems to be actually
 * *capable* to do function inlining, in which case inline function bodys
 * do make sense. we also define G_INLINE_FUNC to properly export the
 * function prototypes if no inlining can be performed.
 * we special case most of the stuff, so inline functions can have a normal
 * implementation by defining G_INLINE_FUNC to extern and G_CAN_INLINE to 1.
 */
#ifndef G_INLINE_FUNC
#  define G_CAN_INLINE 1
#endif
#ifdef G_HAVE_INLINE // compiler supports the __inline__ specifier
#  if defined (__GNUC__) && defined (__STRICT_ANSI__)
#    undef inline
#    define inline __inline__
#  endif
#else /* !G_HAVE_INLINE */
#  undef inline
#  if defined (G_HAVE___INLINE__)
#    define inline __inline__
#  else /* !inline && !__inline__ */
#    if defined (G_HAVE___INLINE) // compiler supports the __inline specifier
#      define inline __inline
#    else /* !inline && !__inline__ && !__inline */
#      define inline /* don't inline, then */
#      ifndef G_INLINE_FUNC
#    undef G_CAN_INLINE
#      endif
#    endif
#  endif
#endif
#ifndef G_INLINE_FUNC
#  ifdef __GNUC__
#    ifdef __OPTIMIZE__
#      define G_INLINE_FUNC extern inline
#    else
#      undef G_CAN_INLINE
#      define G_INLINE_FUNC extern
#    endif
#  else /* !__GNUC__ */
#    ifdef G_CAN_INLINE
#      define G_INLINE_FUNC static inline
#    else
#      define G_INLINE_FUNC extern
#    endif
#  endif /* !__GNUC__ */
#endif /* !G_INLINE_FUNC */

他们似乎在非常复杂的配置文件中设置了 G_HAVE_INLINEG_HAVE__INLINE_。有什么方法可以在不使用自动工具的情况下在代码中完成吗?

AC_C_INLINE,有了这个 autoconf 宏,您可以在所有情况下使用 inline,前提是您包括 config.h(生成的 autoconf 的通常名称 header)。