Inline functions and LTO

Posted on 2010-09-08
Last Modified: 2012-05-10
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 however have to say i'm very confused.

I have a situation where I have one library.a and another 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,
Question by:James_h1023
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 53

Accepted Solution

Infinity08 earned 500 total points
ID: 33630434
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.

Author Comment

ID: 33634476
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,
LVL 53

Assisted Solution

Infinity08 earned 500 total points
ID: 33635908
>> 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.

Author Comment

ID: 33636061
Ah ok I get it.

Many thanks for your help.
LVL 53

Expert Comment

ID: 33636084
Glad to be of assistance :)

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question