c++visual-studio-2013cmath

Visual Studio automatically include cmath?


I'm a beginner in C+=, just wrote my Hello World today.

#include <iostream>
int main(){
double x = 6.25;
x = sqrt(x);
std::cout << x;
return 0;
}

This worked in Visual Studio, no error message, while adding:

#include <cmath> 

worked fine as well.

But with a GCC compiler online the previous code returns with

main.cpp: In function 'int main()':
main.cpp:5:12: error: 'sqrt' was
not declared in this scope x = sqrt(x);
                                     ^

Please help, thanks.


Solution

  • There is no auto include behaviour.. by including <iostream> you are indirectly including <cmath>.

    It's just the way that Microsoft implemented the C++ standard library, they wanted to use some <cmath> functions in so they needed to include it in the header file.

    I recommend you read this article.