我有跨平台(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
宏。
我该如何解决这个问题,有没有更好的方法来使代码跨平台?
您收到此错误是因为编译器完全忽略了在预编译标头之前编写的所有内容。您可以将所需内容放在 stdafx.h 中,而不是预编译标头的条件包含。
另请参阅此链接 https://connect.microsoft.com/VisualStudio/feedback/details/506745/visual-studio-2008-c-fatal-error-c1020-unexpected-endif