【问题标题】:Why am I unable to #ifdef stdafx.h?为什么我无法#ifdef stdafx.h?
【发布时间】:2010-11-28 23:30:08
【问题描述】:

我试图在我的 .cpp 文件中包含 2 个特定于平台的 stdafx.h 文件,但是当我尝试 #ifdef 时编译器不满意。

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

您可能想知道为什么我在 Mac 代码中使用 stdafx.h,但目前这并不重要 :)。

当我尝试在 Windows 上编译代码时,我收到:致命错误 C1018。我尝试用#ifdef 将其他头文件包含在同一个文件中,编译器很高兴。因此,看起来 Windows 不喜欢 stdafx.h 被 #ifdef-ed,或者 Windows 只允许 #include stdafx.h 成为文件的第一行。

所以我的问题是,为什么?

凯特

【问题讨论】:

    标签: conditional-compilation stdafx.h


    【解决方案1】:

    这是因为您打开了预编译标头 - 将其关闭,您应该会没事的。

    【讨论】:

      【解决方案2】:

      当编译器包含一个预编译的头文件时,它基本上会“忘记”头文件之前的任何内容。因此,您的 #elif 不再与 #if 匹配。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-15
        • 2011-06-09
        • 2015-05-31
        • 2016-02-21
        • 1970-01-01
        • 1970-01-01
        • 2014-04-15
        相关资源
        最近更新 更多