On inlining of functions across 2 or more object files

Andrew Haley aph@redhat.com
Mon Sep 22 14:04:00 GMT 2008


Rohit Garg wrote:
> Hi all,
> 
> 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. (yes I am using c++). I am
> compiling this like
> 
> $g++ -c file1.cpp
> $g++ -c file2.cpp
> $g++ -shared file1.o file2.o -o final.so
> 
> My question is that the functions which are called from file1.o are
> inlined for speed. And they reside in file2.o. 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?

Yes, and it's even documented in the fine manual:

"5.36 An Inline Function is As Fast As a Macro"

"This combination of `inline' and `extern' has almost the effect of a
macro.  The way to use it is to put a function definition in a header
file with these keywords, and put another copy of the definition
(lacking `inline' and `extern') in a library file.  The definition in
the header file will cause most calls to the function to be inlined.
If any uses of the function remain, they will refer to the single copy
in the library."

Andrew.



More information about the Gcc-help mailing list