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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With