我注意到当你在Visual Studio的标准库中包含一个标题时(例如iostream
,vector
,set
,..),它还包含一些其他标题太
#include <set>
int main()
{
int A[1000];
std::max(3,5);
memset(A,0,sizeof(A));
pow(2,2);
printf("hi");
}
上面的代码编译在Visual Studio中没有任何错误但在gcc中会导致编译错误
我假设Visual Studio默认包含一些库。但在网上搜索后我发现这不是真的。所以我意识到这是因为不同编译器中标准库的不同实现。所以我想知道这个额外包含背后的原因是什么?
我认为set
不使用pow
或printf
。
答案 0 :(得分:2)
这很容易发生。
<set>
将引入<set>
在其声明中使用的任何内容。例如,它会为std::allocator_traits
提取<memory>
。
反过来,<set>
拉入的所有内容都会吸引其他内容。等等......如果其中一个标题包含<cmath>
,您突然得到std::pow
。
答案 1 :(得分:1)
当这些标头需要其他标头工作时会发生这种情况。
完全依赖于实现。就像您需要在自己的代码中包含标头一样,创建这些标头的人员需要在他们的代码中包含其他标头才能使其正常工作。
不要依赖它。始终包含您需要的内容。
答案 2 :(得分:0)
对gcc使用-H参数表明包含set
的结果包括:
. include/c++/set
.. include/c++/bits/stl_tree.h
... include/c++/bits/stl_algobase.h
.... include/c++/i686-w64-mingw32/bits/c++config.h
..... include/c++/i686-w64-mingw32/bits/os_defines.h
..... include/c++/i686-w64-mingw32/bits/cpu_defines.h
.... include/c++/bits/functexcept.h
..... include/c++/bits/exception_defines.h
.... include/c++/bits/cpp_type_traits.h
.... include/c++/ext/type_traits.h
.... include/c++/ext/numeric_traits.h
.... include/c++/bits/stl_pair.h
..... include/c++/bits/move.h
...... include/c++/bits/concept_check.h
.... include/c++/bits/stl_iterator_base_types.h
.... include/c++/bits/stl_iterator_base_funcs.h
..... include/c++/debug/debug.h
.... include/c++/bits/stl_iterator.h
... include/c++/bits/allocator.h
.... include/c++/i686-w64-mingw32/bits/c++allocator.h
..... include/c++/ext/new_allocator.h
...... include/c++/new
....... include/c++/exception
........ include/c++/bits/atomic_lockfree_defines.h
.... include/c++/bits/memoryfwd.h
... include/c++/bits/stl_function.h
.... include/c++/backward/binders.h
.. include/c++/bits/stl_set.h
.. include/c++/bits/stl_multiset.h
.. include/c++/bits/range_access.h
使用Visual Studio的/ showIncludes显示包含set
的结果包括:
VC\INCLUDE\set
VC\INCLUDE\xtree
VC\INCLUDE\xmemory
VC\INCLUDE\xmemory0
VC\INCLUDE\cstdlib
VC\INCLUDE\yvals.h
VC\INCLUDE\xkeycheck.h
VC\INCLUDE\crtdefs.h
VC\INCLUDE\sal.h
VC\INCLUDE\ConcurrencySal.h
VC\INCLUDE\vadefs.h
VC\INCLUDE\use_ansi.h
VC\INCLUDE\stdlib.h
VC\INCLUDE\crtdefs.h
VC\INCLUDE\limits.h
VC\INCLUDE\crtdefs.h
VC\INCLUDE\limits
VC\INCLUDE\ymath.h
VC\INCLUDE\cfloat
VC\INCLUDE\float.h
VC\INCLUDE\crtdefs.h
VC\INCLUDE\crtwrn.h
VC\INCLUDE\crtdefs.h
VC\INCLUDE\climits
VC\INCLUDE\cmath
VC\INCLUDE\math.h
VC\INCLUDE\crtdefs.h
VC\INCLUDE\xtgmath.h
VC\INCLUDE\xtr1common
VC\INCLUDE\cwchar
VC\INCLUDE\wchar.h
VC\INCLUDE\crtdefs.h
VC\INCLUDE\swprintf.inl
VC\INCLUDE\wtime.inl
VC\INCLUDE\xstddef
VC\INCLUDE\cstddef
VC\INCLUDE\stddef.h
VC\INCLUDE\crtdefs.h
VC\INCLUDE\new
VC\INCLUDE\exception
VC\INCLUDE\eh.h
VC\INCLUDE\crtdefs.h
VC\INCLUDE\malloc.h
VC\INCLUDE\crtdefs.h
VC\INCLUDE\string.h
VC\INCLUDE\crtdefs.h
VC\INCLUDE\xutility
VC\INCLUDE\utility
VC\INCLUDE\iosfwd
VC\INCLUDE\cstdio
VC\INCLUDE\stdio.h
VC\INCLUDE\crtdefs.h
VC\INCLUDE\cstring
VC\INCLUDE\crtdbg.h
VC\INCLUDE\crtdefs.h
VC\INCLUDE\type_traits
VC\INCLUDE\xrefwrap
VC\INCLUDE\xatomic0.h
VC\INCLUDE\intrin.h
VC\INCLUDE\crtdefs.h
VC\INCLUDE\setjmp.h
VC\INCLUDE\crtdefs.h
VC\INCLUDE\immintrin.h
VC\INCLUDE\wmmintrin.h
VC\INCLUDE\nmmintrin.h
VC\INCLUDE\smmintrin.h
VC\INCLUDE\tmmintrin.h
VC\INCLUDE\pmmintrin.h
VC\INCLUDE\emmintrin.h
VC\INCLUDE\xmmintrin.h
VC\INCLUDE\mmintrin.h
VC\INCLUDE\crtdefs.h
VC\INCLUDE\ammintrin.h
VC\INCLUDE\mm3dnow.h
VC\INCLUDE\crtdefs.h
VC\INCLUDE\mmintrin.h
VC\INCLUDE\stdexcept
VC\INCLUDE\xstring