找出内联的功能

Jac*_*ock 11 c++ g++ inline visual-c++

使用GCC 4.4或MSVC编译C++时,是否有可能让编译器在内联函数时发出消息?

Cub*_*bbi 2

使用 g++,我认为您不能让 g++ 报告这一点,但您可以使用任何显示符号的工具检查生成的二进制文件,nm例如:

#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
Run Code Online (Sandbox Code Playgroud)

#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
Run Code Online (Sandbox Code Playgroud)

(在第二种情况下 nm 没有输出)

编辑:此外,分析器可能有用。gprof 在这两个示例中显示:

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
Run Code Online (Sandbox Code Playgroud)

与只是

0.00      0.00     0.00        1     0.00     0.00  global constructors keyed to main
Run Code Online (Sandbox Code Playgroud)

  • FWIW,根据函数的使用方式,编译器可能会内联某些实例而不是其他实例。如果程序足够大,则需要更复杂的东西。 (5认同)