Saeid
Saeid

Reputation: 4265

Extra includes in visual studio's standard library

I have noticed when you include one of the headers in the standard library in Visual Studio (such as iostream, vector, set, .. ) it also includes some other headers too.

#include <set>

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

above code compiles without any error in Visual Studio but in gcc it results in compilation error

I assumed that Visual Studio includes some libraries by default. But after searching in the internet I found out that it's not true. So I realized this is because of the different implementation of the standard library in different compilers. So I want to know what is the reason behind this extra includes? I don't think set uses pow or printf.

Upvotes: 0

Views: 689

Answers (3)

Saeid
Saeid

Reputation: 4265

Using -H parameter for gcc shows that including set results in including:

. 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

And using /showIncludes for Visual Studio show that including set results in including:

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

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385405

It happens when those headers need the other headers to work.

It is completely implementation-dependent. Just as you need to include headers in your own code, the people who create these headers need to include other headers in their code to make it work.

Don't rely on it. Always include what you need.

Upvotes: 1

orlp
orlp

Reputation: 117926

This is very easy to occur.

<set> will pull in anything that <set> uses in its declarations. For example, it will pull in <memory> for std::allocator_traits.

In turn, everything that <set> pulls in will pull other stuff in. Etc... If one of those headers includes <cmath> you've suddenly got std::pow.

Upvotes: 2

Related Questions