extern inline 有什么作用?

2021-12-01 00:00:00 c c++ extern inline-functions

我了解 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.

我认为 static inline 做同样的事情(可能会也可能不会内联),但在内联时不会产生可链接的目标代码(因为没有其他模块可以链接到它).

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).

extern inline 在哪里适合图片?

假设我想用内联函数替换预处理器宏并要求该函数被内联(例如,因为它使用了 __FILE____LINE__ 宏,它们应该解析对于调用者,但不是这个被调用的函数).也就是说,如果函数没有被内联,我希望看到编译器或链接器错误.extern inline 会这样做吗?(我假设,如果没有,除了坚持使用宏之外,没有办法实现这种行为.)

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&R C 或 C89 中,内联不是语言的一部分.许多编译器将其实现为扩展,但没有关于它如何工作的定义语义.GCC 是最先实现内联的,并引入了 inlinestatic inlineextern inline 结构;大多数 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.

  • inline:函数可以被内联(不过这只是一个提示).外部版本总是发出并且外部可见.因此,您只能在一个编译单元中定义这样的内联,而其他每个编译单元都需要将其视为外联函数(否则您将在链接时得到重复的符号).
  • extern inline 不会生成外联版本,但可能会调用一个(因此您必须在其他某个编译单元中定义它.不过,一个定义规则适用;out-of-line 版本必须具有与此处提供的内联相同的代码,以防编译器调用它.
  • static 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:像GNU89extern inline";不发出任何外部可见的函数,但可能会被调用,因此必须存在
  • extern inline:像GNU89内联":发出外部可见的代码,所以最多一个翻译单元可以使用它.
  • static inline:类似于 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

在任何地方内联的函数必须在任何地方内联,具有相同的定义.编译器/链接器将整理出符号的多个实例.static inlineextern inline 没有定义,尽管许多编译器都有它们(通常遵循 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).

相关文章