c++ - header 包含 std c++ 库后的 undefined symbol

标签 c++ g++ c-preprocessor std nm

#include <iostream>

int main()
{
    std::cout << 1.0;
    return 0;
}

我使用命令 g++ -E main.cpp用于在预处理器运行后获取此编译单元。 预处理后它包含大约 20k 行。 它包含这部分代码,定义为 operator<<(double __f) .

namespace std __attribute__ ((__visibility__ ("default")))
{

# 55 "/usr/include/c++/4.7/ostream" 3
  template<typename _CharT, typename _Traits>
    class basic_ostream : virtual public basic_ios<_CharT, _Traits>
    {
    public:

      typedef _CharT char_type;
      typedef typename _Traits::int_type int_type;
      typedef typename _Traits::pos_type pos_type;
      typedef typename _Traits::off_type off_type;
      typedef _Traits traits_type;


      typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
      typedef basic_ios<_CharT, _Traits> __ios_type;
      typedef basic_ostream<_CharT, _Traits> __ostream_type;
      typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> >
             __num_put_type;
      typedef ctype<_CharT> __ctype_type;
# 81 "/usr/include/c++/4.7/ostream" 3
      explicit
      basic_ostream(__streambuf_type* __sb)
      { this->init(__sb); }

      //.........................................................

      __ostream_type&
      operator<<(double __f)
      { return _M_insert(__f); }

      //.........................................................
    };

}

接下来,运行 g++ -c main.cppnm main.o

000000000000005a t _GLOBAL__sub_I_main
000000000000001d t _Z41__static_initialization_and_destruction_0ii
                 U _ZNSolsEd
                 U _ZNSt8ios_base4InitC1Ev
                 U _ZNSt8ios_base4InitD1Ev
                 U _ZSt4cout
0000000000000000 r _ZStL19piecewise_construct
0000000000000000 b _ZStL8__ioinit
                 U __cxa_atexit
                 U __dso_handle
0000000000000000 T main

_ZNSolsEdoperator<<(double) 的名称那条线有"U" The symbol is undefined .但是此运算符的定义在已编译的源代码中。这是什么原因?

最佳答案

这一行:

extern template class basic_ostream<char>;

告诉编译器该函数已经在其他地方可用,它不应该为它发出任何代码(除了可能的内联),就像 C99 中的 inline 一样。

关于c++ - header 包含 std c++ 库后的 undefined symbol ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25584309/

相关文章:

c++ - 静态链接ActiveMQ-cpp

c++ - g++中 `main( )`的链接是什么

c - 使用 C 预处理器为 scanf 构造字符串文字?

c++ - 线程函数可以访问成员变量吗?

c++ - async_eb_func 对比async_tm_func

android - 尝试使用 -pg [Termux] 时出现 "Undefined reference to ` mcount '"

c - 在 C 中初始化数组时出错

c - 查看扩展的 C 宏

c++ - stringstream clear 和 str 有什么区别

c++ - 包含特征向量的类的奇怪大小