問題描述
我了解 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
:像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 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).
這篇關于extern inline 有什么作用?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!