This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Automatic accessor function inlining
- From: Chung-Ju Wu <jasonwucj at gmail dot com>
- To: LRN <lrn1986 at gmail dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Sat, 16 Mar 2013 14:28:39 +0800
- Subject: Re: Automatic accessor function inlining
- References: <5142F29A.3000808@gmail.com>
2013/3/15 LRN <lrn1986@gmail.com>:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> file1.c:
> static int _foobar;
> int get_foobar () { return _foobar; }
>
> file1.h:
> int get_foobar ();
>
> file2.c:
> #include <stdio.h>
> #include "file1.h"
> int main (int argc, char **argv)
> {
> int x = get_foobar ();
> printf ("x = %d\n", x);
> return 0;
> }
>
> Question: will gcc inline get_foobar()?
Hi, LRN,
Like Tobias Burnus said in previous mail,
because get_foobar is in a different object file,
the compiler has no chance to see it without LTO.
But for some cases, you might be able to inline a function
with external linkage by using *inline definition*.
According to C99 6.7.4 point 6~8, a function in a translation unit
includes 'inline' specifier without 'extern' is a *inline definition*.
It does not provide any physical definition for external use,
and does not forbid an external definition in another translation unit.
It just tells compiler how the inline function looks like
in case the compiler decides to perform inlining.
Here is an example of using inline definition:
inline-func.c:
int func (int a, int b)
{
return a + b;
}
inline-func.h:
inline int func (int a, int b)
{
return a + b;
}
inline-main.c:
#include "inline-func.h"
int main()
{
int a, b, c;
a = 3;
b = 6;
c = func (a, b);
return c;
}
Try to compile above programs by following commands:
$ gcc inline-main.c inline-func.c -std=c99 -O0 -S
$ gcc inline-main.c inline-func.c -std=c99 -O2 -S
Check inline-main.s and you will see the difference.
Best regards,
jasonwucj