Re: On inlining of functions across 2 or more object files

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

 



Hi Rohit,

> I have written a small app. Which needs some inline functions. I have
> kept the inline functions in a separate file so that they are reusable
> in the future. The core code resides in, say file1.cpp and the inline
> function definitions reside in file2.cpp.

So when you want your inline functions that are in file2.cpp, in (say) the
foo.cpp file's object file (foo.o), do you do this in foo.cpp...

#incude "file2.cpp"

...?

> My question is that the functions which are called from file1.o are
> inlined for speed. And they reside in file2.o.

That does not make sense.  If the functions are inlined, then they are in
file1.o ... they don't reside in file2.o.

Inlining is as-if you've taken the body of the inline function and
cut-n-pasted it into where that inline function is called.  No calling takes
place -- the inlined function is expanded at that location.

> Now, the gcc docs say
> inline functions are as fast as a macro. My question is, does the
> inliine expansion work even when code is distributed across different
> object files?

Sure.  The inline functions are expanded inline in all the .cpp files that
use them (provided the inline function does not exceed the compiler cap for
inline function expansion).

> 1) My (naive) view tells me, no it is not possible. At the third
> stage, you are just linking. Code expansion happens at the compiling
> and assembling stage. Is this correct?

The inline expansion happens during compiling, not linking.

> 2) I am assuming, whatever the actual behavior is, it is same for both
> c and c++ compilers. I am pretty sure this _is_ correct. But a
> confirmation i welcome.

Correct for C++.  Not sure about C, I'm out of the loop for C.

> Under any circumstances, it would be unacceptable for me to have an
> inline function silently expanded to a "true" function.

That can happen if the inline expansion is too big.

You can guarantee that inline expansion using always_inline.

You can make sure the compiler does not generate an out-of-line function by
using the -fno-keep-inline-functions.

Remember that an inline function is implicitly a static function (and if
there is a non-inline function generated, it has vague linkage).  It's only
compiled into the current translation unit.

You are responsible for not violating ODR.

HTH,
--Eljay


[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