【问题标题】:How to use/avoid #pragma inside #ifdef statement如何在#ifdef 语句中使用/避免#pragma
【发布时间】:2016-06-03 05:44:54
【问题描述】:

我有跨平台(Windows 和 Linux)使用的代码。在 Windows 中,代码使用预编译的头文件,所以我包含 stdafx.h,但在 linux 中我没有。所以我在每个 .cpp 文件的顶部都有这个:

#ifdef WIN32
#include "stdafx.h"
#endif // WIN32  // compiler error here

这在 linux 上编译得很好,但在 Visual Studio 2013 中我得到编译器错误:

错误 2 错误 C1020:意外 #endif

我认为这是因为stdafx.h 包含一个#pragma once 宏。

我该如何解决这个问题,是否有更好的方法使代码跨平台?

【问题讨论】:

标签: c++ visual-studio


【解决方案1】:

您收到此错误是因为编译器完全忽略了在预编译标头之前编写的所有内容。您可以将所需的东西放在 stdafx.h 中,而不是预编译头文件的条件包含。

也请看这个链接https://connect.microsoft.com/VisualStudio/feedback/details/506745/visual-studio-2008-c-fatal-error-c1020-unexpected-endif

【讨论】:

    猜你喜欢
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-20
    • 2018-12-23
    • 2020-09-19
    • 1970-01-01
    相关资源
    最近更新 更多