extern内联有什么作用? [英] What does extern inline do?

查看:167
本文介绍了extern内联有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 inline 本身是对编译器的建议,它可以自行决定是否内联函数,也可以生成可链接的目标代码。 。

I understand that inline by itself is a suggestion to the compiler, and at its discretion it may or may not inline the function, and it will also produce linkable object code.

我认为静态内联的作用相同(可能是内联,也可能不是),但是不会产生可链接对象内联代码(因为没有其他模块可以链接到它)。

I think that static inline does the same (may or may not inline) but will not produce linkable object code when inlined (since no other module could link to it).

外部内联在图片中的位置?

假设我想用内联函数替换预处理器宏,并要求该函数内联(例如,因为它使用了 __ FILE __ __ LINE __ 宏,它们应为调用方解析,但不能为调用的函数解析)。也就是说,如果函数未内联,我想查看编译器或链接器错误。 外部内联会这样做吗? (我假设,如果不这样做,除了坚持使用宏之外,没有其他方法可以实现此行为。)

Assume I want to replace a preprocessor macro by an inline function and require that this function gets inlined (e.g., because it uses the __FILE__ and __LINE__ macros which should resolve for the caller but not this called function). That is, I want to see a compiler or linker error in case the function does not get inlined. Does extern inline do this? (I assume that, if it does not, there is no way to achieve this behavior other than sticking with a macro.)

C ++和C之间是否有区别?

Are there differences between C++ and C?

不同的编译器供应商和版本之间是否存在差异?

Are there differences between different compiler vendors and versions?

推荐答案

K& RC或C89(内联)不是该语言的一部分。许多编译器将其实现为扩展,但没有定义有关其工作方式的语义。 GCC率先实施内联,并引入了 inline 静态内联 extern内联构造;大多数C99之前的编译器通常都遵循其领先地位。

in K&R C or C89, inline was not part of the language. Many compilers implemented it as an extension, but there were no defined semantics regarding how it worked. GCC was among the first to implement inlining, and introduced the inline, static inline, and extern inline constructs; most pre-C99 compiler generally follow its lead.


  • 内联:该函数可以内联(尽管只是一个提示)。脱机版本始终会发出并在外部可见。因此,您只能在一个编译单元中定义这种内联,而其他每个内联都需要将其视为一种外联函数(否则您将在链接时看到重复的符号)。

  • extern inline 不会生成离线版本,但可能会调用一个版本(因此您必须在其他编译单元中定义该版本。)规则适用;不过,离线版本必须与此处提供的内联代码具有相同的代码,以防编译器调用。

  • 静态内联不会生成外部可见的脱机版本,尽管它可能会生成静态的文件,但一定义规则不适用,因为从不发出外部符号,也不会调用

  • inline: the function may be inlined (it's just a hint though). An out-of-line version is always emitted and externally visible. Hence you can only have such an inline defined in one compilation unit, and every other one needs to see it as an out-of-line function (or you'll get duplicate symbols at link time).
  • extern inline will not generate an out-of-line version, but might call one (which you therefore must define in some other compilation unit. The one-definition rule applies, though; the out-of-line version must have the same code as the inline offered here, in case the compiler calls that instead.
  • static inline will not generate a externally visible out-of-line version, though it might generate a file static one. The one-definition rule does not apply, since there is never an emitted external symbol nor a call to one.

  • inline :类似于GNU89 extern inline;不会发出外部可见的函数,但是可能会调用一个函数,因此必须存在

  • extern in行:就像GNU89的内联:发出外部可见的代码,因此最多一个翻译单元可以使用它。

  • 静态内联:类似于GNU89静态内联。这是介于gnu89和c99之间的唯一便携式计算机。

  • inline: like GNU89 "extern inline"; no externally visible function is emitted, but one might be called and so must exist
  • extern inline: like GNU89 "inline": externally visible code is emitted, so at most one translation unit can use this.
  • static inline: like GNU89 "static inline". This is the only portable one between gnu89 and c99

在任何地方都内联的函数必须在任何地方都具有相同的定义的内联。编译器/链接器将整理符号的多个实例。尽管许多编译器都有它们(通常遵循gnu89模型),但没有静态内联外部内联的定义。

A function that is inline anywhere must be inline everywhere, with the same definition. The compiler/linker will sort out multiple instances of the symbol. There is no definition of static inline or extern inline, though many compilers have them (typically following the gnu89 model).

这篇关于extern内联有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆