extern inline 有什么作用?

我了解 inline 本身是对编译器的建议,它可以自行决定是否内联函数,并且还会生成可链接的目标代码.

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

extern inline 在哪里适合图片?

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

C++ 和 C 之间有区别吗?

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

meihaoyong 回答:extern inline 有什么作用?

在 K&R C 或 C89 中,内联不是语言的一部分.许多编译器将其实现为扩展,但没有关于它如何工作的定义语义.GCC 是最先实现内联的,并引入了 inline、static inline 和 extern inline 结构;大多数 C99 之前的编译器通常都跟随它的步伐.

  • 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 inline 或 extern inline 没有定义,尽管许多编译器都有它们(通常遵循 gnu89 模型).

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

本文链接:https://www.f2er.com/3188529.html

大家都在问