inline specifier working if only in header or only in source or in both - Page 1
Forum section: electronics / general

author: ricko_uk
author_rank: Super Contributor
timestamp: November 09, 2021, 09:29:36 am
content: Hi,I just tried and if the inline function specifier is present only in the function definition or only in the declaration or in both files, then the function becomes inline.I thought that, in C, the compiler should give a "mismatch" error if the inline specifier is present in ONLY in the function definition (source file) or ONLY in the declaration (header file). Otherwise, when reading the code someone either must always check every function's declarationanddefinition or, if he does not, then he might not be aware the function has been made inline.Is it like that by design and am missing something in the logic? Or is it just a left-over from legacy (perhaps for backwards compatibility)?Thank you

author: mfro
author_rank: Regular Contributor
timestamp: November 09, 2021, 11:42:29 am
content: that basically depends on whether the compiler sees a "static" keyword or not. If the function to be inlined is not static, it will be - by definition - extern. If it is extern, the compiler needs to make it available as a callable - i.e. not inlined. In the extreme case, you'll end up with two separate incarnatons of the function: one inlined and a second - identical one - as a "real" function.If you want to have the function inlined in all cases, declare and implement it in a header file as static inline.Note that even then it might not become inline in all cases. If you happen to take the function's address (=function pointer) somewhere, the compiler will be forced to implement it as a callable as well.

author: SiliconWizard
author_rank: Super Contributor
timestamp: November 09, 2021, 05:22:02 pm
content: This inline thing has been discussed in several threads here already, you should definitely have a look. And you're in for some surprises and occasional heated "debate". But just so we avoid going all over it again.As just a very quick summary, the 'inline' qualifier in C does not usually do what you think it does.

author: Siwastaja
author_rank: Super Contributor
timestamp: November 09, 2021, 05:30:50 pm
content: Quote from: mfro on November 09, 2021, 11:42:29 amDon't assume it's only such special operations which prevent inlining.Actual modern optimizing compilers arbitrarily decide what they inline and what they do not inline, and the inline qualifier has little to do with this thought process.If you actually really need to get a function inlined - which is sometimes the case in low-level embedded code - at very least use -Winline -Werror (on GCC) or similar (to error out when the compiler decides not to inline). Because rarely we closely examine the full assembly or symbol listing, the extent of not inlining is revealed to you only with -Winline, and then you may want to start using attribute("always_inline") or similar, again depending on compiler.

author: TheCalligrapher
author_rank: Regular Contributor
timestamp: November 10, 2021, 05:22:11 am
content: Quote from: ricko_uk on November 09, 2021, 09:29:36 amFirstly, what programming language are you talking about? `inline` in C is very different from `inline` in C++. Until you state the language, your question is completely nonsensical.Secondly, what "files" are you talking about? Why are you talking about "files"? If it is about thisQuote from: ricko_uk on November 09, 2021, 09:29:36 amthen it won't work at all. You cannot split inline functions into "header" and "source" files. The full definition of an inline function is supposed to be visible everywhere it is used. So, the full definition normally goes into the header file. There's no "source" file.