views:

114

answers:

1

When compiling C++ with GCC 4.4 or MSVC is it possible to get the compiler to emit messages when a function is inlined?

+2  A: 

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
Cubbi
FWIW, depending on how the functions are used, the compiler may inline some instances and not others. If the program is sufficiently large, something more sophisticated would be necessary.
Cogwheel - Matthew Orlando
Indeed. I wonder if any profilers report something like that (gprof doesn't appear to). Could be a useful little tool to write.
Cubbi
Thanks. I can see differences in the compiled binaries. The header only version seems to have inlined more functions. I ran on gprof before (compiled with -pg) and couldn't much differences between the two. I just ran the programs on callgrind and header only version appears to have inlined at lot of functions as they don't show up in callgrinds output.
Jack Nock