问题描述
我理解 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 是最早实现内联的,并引入了 inline
、static inline
和 extern 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
:类似于 GNU89 extern inline";没有发出外部可见的函数,但可能会调用一个函数,因此必须存在extern inline
:类似于 GNU89 inline":发出外部可见的代码,所以最多一个翻译单元可以使用它.静态内联
:类似于 GNU89 静态内联".这是 gnu89 和 c99 之间唯一的便携式
inline
: like GNU89 "extern inline"; no externally visible function is emitted, but one might be called and so must existextern 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 模型).
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).
这篇关于外部内联做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!