Automatic accessor function inlining
Tobias Burnus
burnus@net-b.de
Fri Mar 15 10:29:00 GMT 2013
LRN wrote:
> file1.c:
> static int _foobar;
> int get_foobar () { return _foobar; }
> file2.c:
...
> int x = get_foobar ();
> Question: will gcc inline get_foobar()?
Without LTO (link-time optimization): No. get_foobar is in a different
object file and the compiler has no chance to see this.
That's different if you use instead a macro in the header file (which by
construction always is always inlined). The chances for inlining also
increase, if you place the function in the header file (especially if
you annotate the function to tell the compiler that it should be inlined).
With LTO, the compiler has the chance to inline functions across
'translation units', i.e. different object files. Whether it does
depends - as with all inlining - on the optimization options (e.g. -O0
won't inline, -Os often does not inline [optimization for code size],
but in your case it probably would) and on the code-size growth with
inlining (inlining heuristic, tunable by some flags and parameters).
For LTO, see:
http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-fwhole_002dprogram-890
LTO is supported since GCC 4.5.
Tobias
More information about the Gcc-help
mailing list