2014-08-30 91 views
2
#include <iostream> 

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

我使用命令g++ -E main.cpp在預處理器運行後獲取此編譯單元。 預處理後,它包含約20k行。 它包含這部分代碼,定義爲operator<<(double __f)標準包含std C++庫之後未定義的符號

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。但是這個操作符的定義在編譯源代碼中。這是什麼原因?

+1

是否有'extern模板'某處?請注意'nm -C'更具可讀性。 – 2014-08-30 16:19:18

+0

@ 0x499602D2沒有錯誤消息,除非您以某種方式阻止編譯器將'-lstdC++'傳遞給鏈接器。 – 2014-08-30 16:23:18

回答

1

這條線:

extern template class basic_ostream<char>; 

告訴編譯器的功能已經可用在其他地方,它不應該發出它的任何代碼(可能除了內聯),如inline確實在C99。