【问题标题】:Why does iostream include time.h?为什么 iostream 包含 time.h?
【发布时间】:2011-12-22 10:21:42
【问题描述】:

考虑这段代码:

#include <iostream>

template<class C>
struct time { };

int main() { }

它产生(GCC 4.5):

error: ‘template<class C> struct time’ redeclared as different kind of symbol
/usr/include/time.h:186:15: error: previous declaration of ‘time_t time(time_t*)’
  1. 为什么iostream 包含time_t time(time_t*)
  2. 为什么iostreamstd 命名空间之外包含time_t time(time_t*)
  3. (未回答)为什么,如果我删除 template&lt;class C&gt;,我不会收到此错误?

【问题讨论】:

    标签: c++ namespaces compiler-errors std


    【解决方案1】:

    显然 GCC 的 iostream 需要 time 中的某些内容。

    time_tC 语言的东西,它没有命名空间,因此为了向后比较,它在 std 命名空间中,而不是在 std 命名空间中。

    至于为什么没有错误,我不能 100% 确定。我知道C 可以有同名的函数和结构,并且structs 用名称struct 限定,以便编译器可以区分它们。 C++ 编译器认为它是一个函数,除非前面有 struct 关键字。 http://ideone.com/XZB2M v http://ideone.com/kWMKE。我想它也允许向后比较?

    【讨论】:

    • phs 说它包含 ctime 用于语言环境支持,这有点道理。
    • 我完全不知道那个。对不起:(
    【解决方案2】:

    如果你运行g++ -H -Wall -c testim.cctestim.cc 是你的例子)你会看到

    .... /usr/include/c++/4.6/bits/ios_base.h
    ..... /usr/include/c++/4.6/ext/atomicity.h
    ...... /usr/include/c++/4.6/x86_64-linux-gnu/./bits/gthr.h
    ....... /usr/include/c++/4.6/x86_64-linux-gnu/./bits/gthr-default.h
    ........ /usr/include/pthread.h
    ......... /usr/include/sched.h
    .......... /usr/lib/gcc/x86_64-linux-gnu/4.6/include/stddef.h
    .......... /usr/include/time.h
    

    所以 &lt;time.h&gt; 包含在 pthread 支持中。

    这是 Debian/Sid/AMD64 上的 GCC 4.6

    【讨论】:

    • 在 4.6.2 上试用。更新日志说对&lt;cstddef&gt; 的依赖已经减少。
    • 在我的机器上 g++ 是gcc version 4.6.2 (Debian 4.6.2-3)
    【解决方案3】:

    似乎是为了在我的盒子上支持语言环境:

    $ cat whytime.cc 
    #define _TIME_H_
    #include <iostream>
    
    int main() {
      std::cout << "hello, world" << std::endl;
      return 0;
    }
    
    $ g++ -Wall -Werror -Wextra -ansi -o whytime whytime.cc && ./whytime
    In file included from /usr/include/c++/4.2.1/cwchar:52,
                     from /usr/include/c++/4.2.1/bits/postypes.h:46,
                     from /usr/include/c++/4.2.1/iosfwd:49,
                     from /usr/include/c++/4.2.1/ios:43,
                     from /usr/include/c++/4.2.1/ostream:45,
                     from /usr/include/c++/4.2.1/iostream:45,
                     from whytime.cc:2:
    /usr/include/c++/4.2.1/ctime:66: error: ‘::clock_t’ has not been declared
    /usr/include/c++/4.2.1/ctime:68: error: ‘::tm’ has not been declared
    /usr/include/c++/4.2.1/ctime:70: error: ‘::clock’ has not been declared
    /usr/include/c++/4.2.1/ctime:71: error: ‘::difftime’ has not been declared
    /usr/include/c++/4.2.1/ctime:72: error: ‘::mktime’ has not been declared
    /usr/include/c++/4.2.1/ctime:73: error: ‘::time’ has not been declared
    /usr/include/c++/4.2.1/ctime:74: error: ‘::asctime’ has not been declared
    /usr/include/c++/4.2.1/ctime:75: error: ‘::ctime’ has not been declared
    /usr/include/c++/4.2.1/ctime:76: error: ‘::gmtime’ has not been declared
    /usr/include/c++/4.2.1/ctime:77: error: ‘::localtime’ has not been declared
    /usr/include/c++/4.2.1/ctime:78: error: ‘::strftime’ has not been declared
    In file included from /usr/include/c++/4.2.1/locale:46,
                     from /usr/include/c++/4.2.1/bits/ostream.tcc:46,
                     from /usr/include/c++/4.2.1/ostream:572,
                     from /usr/include/c++/4.2.1/iostream:45,
                     from whytime.cc:2:
    /usr/include/c++/4.2.1/bits/locale_facets.tcc: In member function ‘_InIter std::time_get<_CharT, _InIter>::_M_extract_via_format(_InIter, _InIter, std::ios_base&, std::_Ios_Iostate&, tm*, const _CharT*) const’:
    /usr/include/c++/4.2.1/bits/locale_facets.tcc:1839: error: invalid use of incomplete type ‘struct tm’
    /usr/include/wchar.h:152: error: forward declaration of ‘struct tm’
    /usr/include/c++/4.2.1/bits/locale_facets.tcc:1846: error: invalid use of incomplete type ‘struct tm’
    /usr/include/wchar.h:152: error: forward declaration of ‘struct tm’
    /usr/include/c++/4.2.1/bits/locale_facets.tcc:1854: error: invalid use of incomplete type ‘struct tm’
    /usr/include/wchar.h:152: error: forward declaration of ‘struct tm’
    /usr/include/c++/4.2.1/bits/locale_facets.tcc:1861: error: invalid use of incomplete type ‘struct tm’
    /usr/include/wchar.h:152: error: forward declaration of ‘struct tm’
    /usr/include/c++/4.2.1/bits/locale_facets.tcc:1873: error: invalid use of incomplete type ‘struct tm’
    /usr/include/wchar.h:152: error: forward declaration of ‘struct tm’
    /usr/include/c++/4.2.1/bits/locale_facets.tcc:1880: error: invalid use of incomplete type ‘struct tm’
    /usr/include/wchar.h:152: error: forward declaration of ‘struct tm’
    /usr/include/c++/4.2.1/bits/locale_facets.tcc:1883: error: invalid use of incomplete type ‘struct tm’
    /usr/include/wchar.h:152: error: forward declaration of ‘struct tm’
    /usr/include/c++/4.2.1/bits/locale_facets.tcc:1895: error: invalid use of incomplete type ‘struct tm’
    /usr/include/wchar.h:152: error: forward declaration of ‘struct tm’
    /usr/include/c++/4.2.1/bits/locale_facets.tcc:1900: error: invalid use of incomplete type ‘struct tm’
    /usr/include/wchar.h:152: error: forward declaration of ‘struct tm’
    /usr/include/c++/4.2.1/bits/locale_facets.tcc:1908: error: invalid use of incomplete type ‘struct tm’
    /usr/include/wchar.h:152: error: forward declaration of ‘struct tm’
    /usr/include/c++/4.2.1/bits/locale_facets.tcc:1912: error: invalid use of incomplete type ‘struct tm’
    /usr/include/wchar.h:152: error: forward declaration of ‘struct tm’
    /usr/include/c++/4.2.1/bits/locale_facets.tcc:1932: error: invalid use of incomplete type ‘struct tm’
    /usr/include/wchar.h:152: error: forward declaration of ‘struct tm’
    /usr/include/c++/4.2.1/bits/locale_facets.tcc:1968: error: invalid use of incomplete type ‘struct tm’
    /usr/include/wchar.h:152: error: forward declaration of ‘struct tm’
    /usr/include/c++/4.2.1/bits/locale_facets.tcc:1976: error: invalid use of incomplete type ‘struct tm’
    /usr/include/wchar.h:152: error: forward declaration of ‘struct tm’
    /usr/include/c++/4.2.1/bits/locale_facets.tcc: In member function ‘virtual _InIter std::time_get<_CharT, _InIter>::do_get_weekday(_InIter, _InIter, std::ios_base&, std::_Ios_Iostate&, tm*) const’:
    /usr/include/c++/4.2.1/bits/locale_facets.tcc:2210: error: invalid use of incomplete type ‘struct tm’
    /usr/include/wchar.h:152: error: forward declaration of ‘struct tm’
    /usr/include/c++/4.2.1/bits/locale_facets.tcc: In member function ‘virtual _InIter std::time_get<_CharT, _InIter>::do_get_monthname(_InIter, _InIter, std::ios_base&, std::_Ios_Iostate&, tm*) const’:
    /usr/include/c++/4.2.1/bits/locale_facets.tcc:2259: error: invalid use of incomplete type ‘struct tm’
    /usr/include/wchar.h:152: error: forward declaration of ‘struct tm’
    /usr/include/c++/4.2.1/bits/locale_facets.tcc: In member function ‘virtual _InIter std::time_get<_CharT, _InIter>::do_get_year(_InIter, _InIter, std::ios_base&, std::_Ios_Iostate&, tm*) const’:
    /usr/include/c++/4.2.1/bits/locale_facets.tcc:2288: error: invalid use of incomplete type ‘struct tm’
    /usr/include/wchar.h:152: error: forward declaration of ‘struct tm’
    

    【讨论】:

    • 通过定义time.h 的标头保护,我们强制它包含不做任何事情。大概任何包含它的东西都会在不久之后使用它,并且由此产生的编译错误会当场抓住包含器。对我来说,这似乎是ostream 带来的/usr/include/c++/4.2.1/locale。应该记住,这只显示一个包含器,可能还有其他的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    • 2014-03-10
    • 1970-01-01
    相关资源
    最近更新 更多