Share via

Auto include header files in C++ - Visual Studio

Shervan360 1,681 Reputation points
2021-03-22T04:37:27.833+00:00

Hello,

I wrote the below code and it ran successfully. Visual Studio automatically include the stdlib.h for rand function.

How can I disable the auto include header file in visual studio? I'd like to disable this feature.

#include<iostream>
using namespace std;

int main() {

    cout<<rand();

}
Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.

0 comments No comments

Answer accepted by question author

WayneAKing 4,936 Reputation points
2021-03-22T05:41:13.33+00:00

Visual Studio automatically include the stdlib.h for
rand function.
How can I disable the auto include header file in
visual studio?

You can't. (Unless you want to risk seriously breaking
the compiler's libraries, headers, etc.)

Tampering with the compiler's headers is a serious
transgression that threatens to destabilize the entire
chain of dependencies. It is a cardinal rule that you
should never tamper with the headers or libraries that
ship with the compiler. In addition to possibly breaking
some aspect of its current implementation, there is the
risk if sabotaging future patches, updates, etc.

The compiler may auto-include stdlib.h via an embedded
include in the iostream header, or in one of the other
headers which it pulls in. When it does it's because the
implementor(s) felt it was necessary for some aspect of
the implementation.

This may change from version to version. Therefore it
is strongly advised that such implicit including
not be relied upon. Rather one should always explicitly
use the #include associated with a given function, etc.
Even when it is already auto-included via another header.

  • Wayne

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.