Author Topic: inline specifier working if only in header or only in source or in both  (Read 1106 times)

0 Members and 1 Guest are viewing this topic.

Offline ricko_ukTopic starter

  • Super Contributor
  • ***
  • Posts: 1015
  • Country: gb
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 declaration and definition 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 :)
 

Offline mfro

  • Regular Contributor
  • *
  • Posts: 210
  • Country: de
Re: inline specifier working if only in header or only in source or in both
« Reply #1 on: November 09, 2021, 11:42:29 am »
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.
Beethoven wrote his first symphony in C.
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14447
  • Country: fr
Re: inline specifier working if only in header or only in source or in both
« Reply #2 on: November 09, 2021, 05:22:02 pm »
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.
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 8168
  • Country: fi
Re: inline specifier working if only in header or only in source or in both
« Reply #3 on: November 09, 2021, 05:30:50 pm »
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.

Don'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.
 

Offline TheCalligrapher

  • Regular Contributor
  • *
  • Posts: 151
  • Country: us
Re: inline specifier working if only in header or only in source or in both
« Reply #4 on: November 10, 2021, 05:22:11 am »
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.

Firstly, 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 this

only in header or only in source

then 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.
« Last Edit: November 10, 2021, 09:20:06 pm by TheCalligrapher »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf