Why am I unable to #ifdef stdafx.h?

2019-02-21 11:15发布

问题:

I am trying to include 2 platform-specific stdafx.h files in my .cpp file, but the compiler is unhappy when I try to #ifdef it.

#ifdef _WIN32
#include "stdafx.h"
#elif _MAC
#include "MAC/stdafx.h"
#endif

You may wonder why I am using stdafx.h in the Mac code, but that is not important at the moment :).

When I try to compile the code on Windows, I receive: Fatal Error C1018. I tried enclosing other header files with #ifdef in the same file, and the compiler was happy. Therefore, it looks like Windows doesn't like stdafx.h to be #ifdef-ed, or that Windows only allows #include stdafx.h to be the first line in the file.

So my question is, why?

Kat

回答1:

When the compiler includes a pre-compiled header, it basically "forgets" anything that came before the header. Thus your #elif isn't matched to a #if anymore.



回答2:

This is because you have Precompiled Headers turned on - turn it off and you should be fine.