Visual Studio 标准库中的额外内容

Extra includes in visual studio's standard library

本文关键字:额外内容 Studio 标准 Visual      更新时间:2023-10-16

我注意到,当您在Visual Studio的标准库中包含其中一个标头(如iostreamvectorset等)时,它还包含其他一些标头。

#include <set>
int main()
{
    int A[1000];
    std::max(3,5);
    memset(A,0,sizeof(A));
    pow(2,2);
    printf("hi");
}

以上代码在Visual Studio中编译时没有任何错误,但在gcc中会导致编译错误

我假设VisualStudio默认包含一些库。但在网上搜索后,我发现这不是真的。所以我意识到这是因为标准库在不同编译器中的实现方式不同。所以我想知道这个额外的包含背后的原因是什么?我不认为set使用powprintf

这很容易发生。

<set>将引入<set>在其声明中使用的任何内容。例如,它将为std::allocator_traits引入<memory>

反过来,<set>拉入的所有内容都会拉入其他内容。等等……如果其中一个标头包含<cmath>,那么你就会突然得到std::pow

当这些标头需要其他标头才能工作时,就会发生这种情况。

它完全依赖于实现。正如您需要在自己的代码中包含头一样,创建这些头的人也需要在他们的代码中包含其他头才能使其工作。

不要依赖它。总是包括你需要的东西。

对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

使用/showIncludes for Visual Studio显示包含set会导致包含:

VCINCLUDEset
 VCINCLUDExtree
  VCINCLUDExmemory
   VCINCLUDExmemory0
    VCINCLUDEcstdlib
     VCINCLUDEyvals.h
      VCINCLUDExkeycheck.h
      VCINCLUDEcrtdefs.h
       VCINCLUDEsal.h
        VCINCLUDEConcurrencySal.h
       VCINCLUDEvadefs.h
      VCINCLUDEuse_ansi.h
     VCINCLUDEstdlib.h
      VCINCLUDEcrtdefs.h
      VCINCLUDElimits.h
       VCINCLUDEcrtdefs.h
    VCINCLUDElimits
     VCINCLUDEymath.h
     VCINCLUDEcfloat
      VCINCLUDEfloat.h
       VCINCLUDEcrtdefs.h
       VCINCLUDEcrtwrn.h
        VCINCLUDEcrtdefs.h
     VCINCLUDEclimits
     VCINCLUDEcmath
      VCINCLUDEmath.h
       VCINCLUDEcrtdefs.h
      VCINCLUDExtgmath.h
       VCINCLUDExtr1common
     VCINCLUDEcwchar
      VCINCLUDEwchar.h
       VCINCLUDEcrtdefs.h
       VCINCLUDEswprintf.inl
       VCINCLUDEwtime.inl
     VCINCLUDExstddef
      VCINCLUDEcstddef
       VCINCLUDEstddef.h
        VCINCLUDEcrtdefs.h
    VCINCLUDEnew
     VCINCLUDEexception
      VCINCLUDEeh.h
       VCINCLUDEcrtdefs.h
      VCINCLUDEmalloc.h
       VCINCLUDEcrtdefs.h
      VCINCLUDEstring.h
       VCINCLUDEcrtdefs.h
    VCINCLUDExutility
     VCINCLUDEutility
      VCINCLUDEiosfwd
       VCINCLUDEcstdio
        VCINCLUDEstdio.h
         VCINCLUDEcrtdefs.h
       VCINCLUDEcstring
       VCINCLUDEcrtdbg.h
        VCINCLUDEcrtdefs.h
      VCINCLUDEtype_traits
       VCINCLUDExrefwrap
    VCINCLUDExatomic0.h
    VCINCLUDEintrin.h
     VCINCLUDEcrtdefs.h
     VCINCLUDEsetjmp.h
      VCINCLUDEcrtdefs.h
     VCINCLUDEimmintrin.h
      VCINCLUDEwmmintrin.h
       VCINCLUDEnmmintrin.h
        VCINCLUDEsmmintrin.h
         VCINCLUDEtmmintrin.h
          VCINCLUDEpmmintrin.h
           VCINCLUDEemmintrin.h
            VCINCLUDExmmintrin.h
             VCINCLUDEmmintrin.h
              VCINCLUDEcrtdefs.h
     VCINCLUDEammintrin.h
     VCINCLUDEmm3dnow.h
      VCINCLUDEcrtdefs.h
      VCINCLUDEmmintrin.h
  VCINCLUDEstdexcept
   VCINCLUDExstring