• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 813
  • Last Modified:

Inline functions and LTO

Hi *,

I'm a little confused with how to take full advantage of GCC's new LTO and some inline functions. I have read websites like http://www.linuxtopia.org/online_books/programming_tool_guides/linux_using_gnu_compiler_collection/inline.html however have to say i'm very confused.

I have a situation where I have one library.a and another library2.so and then some header and source files. Where do I put 'inline', 'extern inline' and 'static inline'? In just the headers, just the source or both? And in what situations!

I'm planning on using '-flto -fuse-linker-plugin -fwhole-program' and have compiled and installed GCC with '--enable-lto --enable-gold' so hopefully it will be able to link and inline/optimise all my code as if it were one (if I understand LTO that is).

Many thanks in advance,
James
0
James_h1023
Asked:
James_h1023
  • 3
  • 2
2 Solutions
 
Infinity08Commented:
inline functions are defined in each compilation unit where they are used.

If only one compilation unit uses the inline function, then you'd generally place the definition in the .c/.cpp file for that compilation unit.

If multiple compilation units use the inline function, and/or if you want to make it available as part of the library API, you'd place the definition in a header file, and include that header file in every compilation unit where it's used.
0
 
James_h1023Author Commented:
Thanks for a quick reply.

The first makes, sense. With the latter, would I also need to place the definition in the .c file as well as the header?

Also where should I use extern and static with regard to inline?

Many thanks,
James
0
 
Infinity08Commented:
>> would I also need to place the definition in the .c file as well as the header?

No, since the definition is in the header file, and since you include the header file in the .c file, the .c file will have all it needs.


>> Also where should I use extern and static with regard to inline?

extern and static determine the linkage of the function (just like for a normal non-inline function) :

'extern inline' will cause object code to be generated for the function (with external linkage), so it can be called from outside the translation unit(s) where it's defined. It provides two ways of using the function : either inlined or as a normal (external) function call.

'static inline' ensures that if object code is generated for the function, then it will have static linkage. Every translation unit where the function is defined can have inlined versions of the function and/or a static normal function.
0
 
James_h1023Author Commented:
Ah ok I get it.

Many thanks for your help.
James
0
 
Infinity08Commented:
Glad to be of assistance :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now