我们的项目使用大量小型单行内联函数来进行简单的算术计算。我们如何才能准确地找出用户的 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_
。有什么方法可以在不使用自动工具的情况下在代码中完成此操作吗?
最佳答案
AC_C_INLINE
,使用此 autoconf 宏,您可以在所有情况下使用 inline
,前提是包含 config.h
(autoconf 生成的 header 的常用名称)。
关于c - 在不使用 AUTOCONF 的情况下,如何找出 C 编译器 : inline, __inline__ 或 __inline 支持哪些说明符(如果有)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51364804/