When compiling C++ with GCC 4.4 or MSVC is it possible to get the compiler to emit messages when a function is inlined?
The decision to inline or not a function is made by compiler. And since it is made by compiler, so YES, it can be made at compile time only. So, if you can see the assembly code by using -S option (with gcc -S produces assembly code), you can see whether your function has been inlined or not. Save this answer.
Only when you want the function to be defined in a header. More exactly only when the function's definition can show up in multiple translation units. It's a good idea to define small (as in one liner) functions in the header file as it gives the compiler more information to work with while optimizing your code.
An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. This eliminates call-linkage overhead and can expose significant optimization opportunities.
Static inline functions are simple. Either a function defined with the inline function specifier is inlined at a reference, or a call is made to the actual function. The compiler can choose which to do at each reference. The compiler decides if it is profitable to inline at -xO3 and above.
With g++, I don't think you can make g++ report that, but you can examine the resulting binary with any tool that shows symbols, nm
for example:
#include <iostream>
struct T {
void print() const;
};
void T::print() const { std::cout << " test\n" ; }
int main()
{
T t;
t.print();
}
~ $ g++ -O3 -Wall -Wextra -pedantic -o test test.cc
~ $ nm test | grep print
0000000000400800 t _GLOBAL__I__ZNK1T5printEv
0000000000400830 T _ZNK1T5printEv
vs
#include <iostream>
struct T {
void print() const { std::cout << " test\n" ; }
};
int main()
{
T t;
t.print();
}
~ $ g++ -O3 -Wall -Wextra -pedantic -o test test.cc
~ $ nm test | grep print
(no output from nm in the second case)
EDIT: Also, profilers may be of use. gprof shows, on these two examples:
0.00 0.00 0.00 1 0.00 0.00 global constructors keyed to _ZNK1T5printEv
0.00 0.00 0.00 1 0.00 0.00 T::print() const
vs. just
0.00 0.00 0.00 1 0.00 0.00 global constructors keyed to main
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