首页 > 解决方案 > 在不使用 AUTOCONF 的情况下,如何准确地找出 C 编译器支持的说明符(如果有):inline、__inline__ 或 __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_INLINE和。G_HAVE__INLINE_有没有办法在不使用自动工具的情况下在代码中做到这一点?

标签: cinlineautoconf

解决方案


AC_C_INLINE,使用此 autoconf 宏,您可以inline在所有情况下使用,前提是您包含config.h(autoconf 生成的标头的常用名称)。


推荐阅读