inline and duplicate definition

from  http://gcc.gnu.org/onlinedocs/gcc-4.3.3/gcc/Inline.html#Inline

  1, "When an inline function is not static, then the compiler must assume that there may be calls from other source files; since a global symbol can be defined only once in any program, the function must not be defined in the other source files, so the calls therein cannot be integrated. Therefore, a non-static inline function is always compiled on its own in the usual fashion."

2, "If you specify both inline and extern in the function definition, then the definition is used only for inlining. In no case is the function compiled on its own, not even if you refer to its address explicitly. Such an address becomes an external reference, as if you had only declared the function, and had not defined it."

from  http://stackoverflow.com/questions/216510/extern-inline/216546#216546

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.

GNU89:

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

C99 (or GNU99):

  • "inline": like GNU "extern inline"; no externally visible function is emitted, but one might be called and so must exist
  • "extern inline": like GNU "inline": externally visible code is emitted, so at most one translation unit can use this.
  • "static inline": like GNU "static inline". This is the only portable one between gnu89 and c99

C++:

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

另外两个值得查阅的链接:

http://gcc.gnu.org/gcc-4.3/porting_to.html

http://www.greenend.org.uk/rjk/tech/inline.html

原文地址:https://www.cnblogs.com/superniaoren/p/3057890.html