简体   繁体   中英

How does the inlining performed with extern inline in C?

As titled.

I've read a couple of posts on StackOverflow regarding extern inline , and still, I do not quite understand how the below pattern works? Some posts say that we need to have the inline definition in the header file, and then the corresponding extern inline declaration in the source file as I showed below.

foo.h:

inline void foo(void) { ... }

foo.c:

#include "foo.h"
extern inline void foo(void);
int main(void) {
  foo();
}

My question is that how does the compiler perform the inlining when compiling with like -O1 flag since, after the preprocessing, the inline definition would be added to the source file, and in this translation unit, we have the extern inline which actually changes the original inline definition of the function foo to an external definition and this cannot be used for inlining at all. Is this understanding correct?

… in this translation unit, we have the extern inline which actually changes the original inline definition of the function foo to an external definition and this cannot be used for inlining at all. Is this understanding correct?

This is correct; C 2018 6.7.4 7 says “… If all of the file scope declarations for a function in a translation unit include the inline function specifier without extern , then the definition in that translation unit is an inline definition …” Since the translation unit you show has a declaration for foo that has extern (it has extern inline void foo(void); ), it does not satisfy those requirements in 6.7.4 7, so it does not qualify as an inline definition.

My question is that how does the compiler perform the inlining when compiling with like -O1 flag since, after the preprocessing, the inline definition would be added to the source file,…

The C standard says nothing about how the compiler does perform inlining, just about what it may do. It says that if there were an inline definition, it would provide an alternative the compiler could choose: The compiler would be allowed by the C standard to use either the inline definition or the external definition.

It is easier to use static inline . Then the function is defined only for the current translation unit, and the function is an inline function , which suggests to the compiler the calls to the function be made fast (typically by integrating its code into the function that calls it).

For external inline functions, there should be one translation unit that provides an external non-inline definition (typically by defining the function in the normal way, without inline , and typically in a source file, not a header). Then every other translation unit that wants to have the option of using an inline version should have one inline definition (typically provided inside a header, not in the source file as you show), and that translation unit should not use extern in any of its declarations of the name of the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM