-1

I created a C++ Console Application in Visual Studio Community 2017. There is only a main.cpp file in the project. Here is my main.cpp file:

#include <iostream>
#include "stdafx.h"

int main()
{
    std::cout << "hello world!";
    return 0;
}

I get a compilation error that 'cout' is not a member of std. But if I include iostream after stdafx.h, that is,

#include "stdafx.h"
#include <iostream>

int main()
{
    std::cout << "hello world!";
    return 0;
}

then it compiles just fine. So why does it not work when I include iostream before stdafx.h?

Arpan Biswas
  • 185
  • 2
  • 12
  • 3
    Possible duplicate of [What's the use for "stdafx.h" in Visual Studio?](https://stackoverflow.com/questions/4726155/whats-the-use-for-stdafx-h-in-visual-studio) – Dean Seo May 02 '18 at 11:03
  • 1
    There are so much questions regarding precompiled headers on SO, that I am not sure which one of them to flag as a duplicate.. – Algirdas Preidžius May 02 '18 at 11:03
  • 1
    In my project, one of the other devs turned off precompiled headers, and cut our build time in half. – Eljay May 02 '18 at 11:06
  • `stdafx.h` is associated with the (Microsoft-specific) way of managing precompiled headers (although other compilers use similar tricks). Including something before it confuses how the compiler handles precompiled headers, and therefore affects how it compiles the content within them. If you prefer to use precompiled headers, don't `#include` anything in your source file before `stdafx.h`. If you turn off precompiled headers, you will not even need to `#include "stdafx.h"` unless your program uses windows-specific API functions - which your sample does not. – Peter May 02 '18 at 11:09
  • 1
    @Eljay That depends on the project.. On the project I am working on - full rebuild takes slightly more than 2 hours. One can't even test how precompiled headers impact build performance, without spending significant amount of time. – Algirdas Preidžius May 02 '18 at 11:13
  • 1
    @Eljay in that case, you more than likely had the wrong headers included in your precompiled. You only really gain for headers that are included multiple times. – UKMonkey May 02 '18 at 11:15
  • @AlgirdasPreidžius • my project is down to 1 hour for a full rebuild, and minutes for an incremental build. Was 2 hours for a full rebuild, and about 2 hours for an incremental build. I whole-heartedly agree with UKMonkey ... the wrong headers are included in the precompiled header set. My project would be improved with a thorough Lakos-ification, even though that guidance is from the mid-90s. Then again, my project is from the late 80s. – Eljay May 02 '18 at 11:52

1 Answers1

3

The answer to your question can be found, with a little puzzling, here.

stdafx.h enables precompiled headers. Based on the error given, and the discussion of how Microsoft implements precompiled headers, it seems that the compiler simply starts compiling from the include of stdafx.h forward. So when stdafx.h is placed after iostream, iostream is not included, producing the mysterious error.

Spacemoose
  • 3,856
  • 1
  • 27
  • 48