目标文件中的 ostream 运算符会发生什么?

What happens to ostream operator in object file?

我想了解目标文件中 class' ostream 运算符 "lives" 的位置。在下面的代码示例中,我设计了 C++ class DeltaTimer 与 ostream 运算符的朋友。下面是 "nm DeltaTimer.o" 的输出。 在此输出中搜索 "ostream" 显示它们针对符号类型 "U",即未定义。但是代码编译(main.cpp 以确保完整性)并成功链接,并按预期执行,这意味着没有未定义的内容。但我不明白 ostream 运算符在 "nm" 的输出中解析了什么,如果有人能提供建议,我将不胜感激。 谢谢。

DeltaTimer.h:

#ifndef DeltaTimer_h
#define DeltaTimer_h

#include <iostream>

class DeltaTimer
{
public:
    DeltaTimer();
    virtual ~DeltaTimer();
    void Foo();

private:
    int m_foo;

friend std::ostream& operator<<(std::ostream& os, const DeltaTimer& rDeltaTimer);
};

#endif // DeltaTimer_h

DeltaTimer.cpp:

#include "DeltaTimer.h"
#include <iostream>

DeltaTimer::DeltaTimer() : m_foo(5)
{
    std::cout << "DeltaTimer ctor" << std::endl;
}

DeltaTimer::~DeltaTimer()
{
    std::cout << "DeltaTimer dtor" << std::endl;
}

void DeltaTimer::Foo()
{
    std::cout << "DeltaTimer.m_foo == " << m_foo << std::endl;
}

std::ostream& operator<<(std::ostream& os, const DeltaTimer& rDeltaTimer)
{
    os << "DeltaTimer[m_foo(" << rDeltaTimer.m_foo << ")]" << std::endl;
}

// This function is just a sanity-check for the output of "nm"
void Bar()
{
    std::cout << "void Bar()" << std::endl;
}

main.cpp:

#include "DeltaTimer.h"
#include <iostream>

int main()
{
    DeltaTimer deltaTimer;

    std::cout << deltaTimer << std::endl;
}

"nm" 的输出:

>nm DeltaTimer.o
00000000000001a7 t _GLOBAL__I__ZN10DeltaTimerC2Ev
0000000000000145 T _Z3Barv
0000000000000167 t _Z41__static_initialization_and_destruction_0ii
00000000000000b0 T _ZN10DeltaTimer3FooEv
0000000000000000 T _ZN10DeltaTimerC1Ev
0000000000000000 T _ZN10DeltaTimerC2Ev
000000000000008a T _ZN10DeltaTimerD0Ev
0000000000000040 T _ZN10DeltaTimerD1Ev
0000000000000040 T _ZN10DeltaTimerD2Ev
                 U _ZNSolsEPFRSoS_E
                 U _ZNSolsEi
                 U _ZNSt8ios_base4InitC1Ev
                 U _ZNSt8ios_base4InitD1Ev
                 U _ZSt4cout
                 U _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
0000000000000000 b _ZStL8__ioinit
                 U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
0000000000000000 V _ZTI10DeltaTimer
0000000000000000 V _ZTS10DeltaTimer
0000000000000000 V _ZTV10DeltaTimer
                 U _ZTVN10__cxxabiv117__class_type_infoE
                 U _ZdlPv
00000000000000f1 T _ZlsRSoRK10DeltaTimer
                 U __cxa_atexit
                 U __dso_handle
                 U __gxx_personality_v0

>nm DeltaTimer.o | grep -i ostream
                 U _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
                 U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc

这是因为 nm 显示的是错位的名称。要打印人类可读的内容,您需要使用 --demangle 参数:

$ nm --demangle DeltaTimer.o 
                 U __cxa_atexit
                 U __dso_handle
00000000000001a4 t _GLOBAL__sub_I__ZN10DeltaTimerC2Ev
                 U __gxx_personality_v0
0000000000000145 T Bar()
0000000000000167 t __static_initialization_and_destruction_0(int, int)
                 U operator delete(void*)
00000000000000f1 T operator<<(std::ostream&, DeltaTimer const&) // <== here's your guy!
00000000000000b0 T DeltaTimer::Foo()
0000000000000000 T DeltaTimer::DeltaTimer()
0000000000000000 T DeltaTimer::DeltaTimer()
000000000000008a T DeltaTimer::~DeltaTimer()
0000000000000040 T DeltaTimer::~DeltaTimer()
0000000000000040 T DeltaTimer::~DeltaTimer()
                 U std::ostream::operator<<(int)
                 U std::ostream::operator<<(std::ostream& (*)(std::ostream&))
                 U std::ios_base::Init::Init()
                 U std::ios_base::Init::~Init()
                 U std::cout
                 U std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)
0000000000000000 r std::piecewise_construct
0000000000000000 b std::__ioinit
                 U std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
0000000000000000 V typeinfo for DeltaTimer
0000000000000000 V typeinfo name for DeltaTimer
0000000000000000 V vtable for DeltaTimer
                 U vtable for __cxxabiv1::__class_type_info

或者只是使用c++filt是为了。具体来说:

$ echo _ZlsRSoRK10DeltaTimer | c++filt
operator<<(std::basic_ostream<char, std::char_traits<char> >&, DeltaTimer const&)