c - 在不使用 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_
有没有办法在不使用自动工具的情况下在代码中做到这一点?
解决方案
AC_C_INLINE
,使用此 autoconf 宏,您可以inline
在所有情况下使用,前提是您包含config.h
(autoconf 生成的标头的常用名称)。
推荐阅读
- r - 修改对象以在 R 中与 summary_table() 一起使用:“错误:`x` 必须是公式”
- python - 基于另一个线程激活一个线程
- r - 将整个文本放入绘图区域
- c++ - 为什么我的第二个 sendinput 实际上没有发送输入?
- javascript - Jquery İnput Keyup 泰语、英语和空间字符
- mysql - Elasticsearch 会更好地与像 MongoDB 这样的文档数据库或像 MySQL 这样的关系数据库一起工作吗?
- python - 如何使用 autouse 全局定义补丁?
- javascript - 在firebase中选择内部循环?
- android-studio - 如何向数字类型的 EditText 添加分数?
- python - Django 索引未考虑现有表