inline and duplicate definition
2013-05-03 21:34
671 查看
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.
"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.
"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
另外两个值得查阅的链接:
http://gcc.gnu.org/gcc-4.3/porting_to.html
http://www.greenend.org.uk/rjk/tech/inline.html
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
相关文章推荐
- Laravel 出现"RuntimeException inEncrypter.php line 43: The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths."问题的解决办法
- Proxy settings in TortoiseSVN and command line svn client
- Html block and inline
- A missing vtable usually means the first non-inline virtual member function has no definition
- rlwrap for Command Line History and Editing in SQL*Plus and RMAN on Linux
- effective C++ Item 2: Prefer consts, enums, and inlines to #defines
- org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 1 in XML document from U
- a missing vtable usually means the first non-inline virtual member function has no definition.
- what is the difference between definition and declaration in c
- Suterusu Rootkit: Inline Kernel Function Hooking on x86 and ARM
- 消息 1033,级别 15 The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table
- How to run java and junit in command line
- IBatisNet1.5 -- 映射文件--Parameter Maps and Inline Parameters
- NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
- 基本图论定义与术语(Basic Definition and Glossary in Graph The)
- LeetCode Find All Duplicates in Array and Find Disappeared Numbers in Array
- Effective C++读书笔记——(一)Prefer consts, enums, and inlines to #defines
- In C++, what’s the difference between an inline function and a macro?
- Relevance Between Variable Declaration and Definition in C++
- Count the number of alphabets, spaces, digits and other characters in one line character