Re: Automatic accessor function inlining

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2013/3/15 LRN <lrn1986@xxxxxxxxx>:
> -----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


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux