Why am I unable to #ifdef stdafx.h?

2019-02-21 11:28发布

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

2条回答
够拽才男人
2楼-- · 2019-02-21 12:01

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.

查看更多
唯我独甜
3楼-- · 2019-02-21 12:04

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

查看更多
登录 后发表回答