Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inlining functions from object files

Tags:

c

gcc

I want to inline some functions of which I don't have the code. They are present in an object file. Is there a way to do it with gcc?

In other words, I want to perform inlining of those functions while linking my own code files with the object file that contain those functions.

like image 848
MetallicPriest Avatar asked Jul 26 '11 16:07

MetallicPriest


People also ask

Can the linker inline functions?

The linker can inline small functions in place of a branch instruction to that function. For the linker to be able to do this, the function (without the return instruction) must fit in the four bytes of the branch instruction.

What are the conditions of inlining a function?

Inline function may increase efficiency if it is small. 2) If a function contains static variables. 3) If a function is recursive. 4) If a function return type is other than void, and the return statement doesn't exist in function body.

How do you check if a function is inlined or not?

If you need to make sure that function is inlined and OK to go with proprietary extension in MS VC++, check out the __forceinline declarator. The compiler will either inline the function or, if it falls into the list of documented special cases, you will get a warning - so you will know the inlining status.

How do you create an inline function?

To inline a function, place the keyword inline before the function name and define the function before any calls are made to the function. The compiler can ignore the inline qualifier in case defined function is more than a line.


1 Answers

Starting with version 4.5, GCC supports the -flto switch which enables Link Time Optimization (LTO). LTO can inline functions from separate object files.

There's a catch though. Because of the way that -flto works, it'll only be of use for object files that were compiled using that switch. As I understand it, GCC implements LTO by placing a intermediate form of the source code into the object file - if that intermediate code isn't in the object file, the code in that object file won't be 'inlined'.

See Can the linker inline functions? for some additional details.

like image 178
Michael Burr Avatar answered Sep 30 '22 12:09

Michael Burr