Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why / how are some c++ headers included by default in visual studio?

In visual studio c++, some headers seems to be included by default. For example, I can use std::strncpy or std::string without including <string> or <cstring>, but I cannot use std::cout or std::min() without including <iostream> or <algorithm>.

Then, when I want to compile the source code on unix with g++, I get compilation error if I forgot to add the includes that visual studio did not warm me about because of its implicit includes.

Where does the default includes come from in visual studio? Is there a way to deactivate this behavior for future and existing projects? Note: my visual studio projects are not using precompiled headers.

like image 459
Fehnryr Avatar asked Jun 17 '19 01:06

Fehnryr


1 Answers

Visual Studio has no headers included by default. However, some headers include other headers, so if you include <iostream>, that may sometimes include <string> in some compilers, and many headers also include <cstring>. So you are including them yourself, by accident.

Which headers includes which other headers varies from library to library, so always explicitly include the headers used by that file.

like image 194
Mooing Duck Avatar answered Oct 05 '22 17:10

Mooing Duck