******* stdafx.h ********
#pragma once
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN
// Windows Header Files:
#include <windows.h>
**************************
******* dummy.cpp ********
#if defined (_MSC_VER)
#include "stdafx.h"
#endif
/........ content ....../
**************************
当我编译 dummy.cpp
时,Visual C++ 编译器弹出以下编译器错误:
错误 C2856:#pragma hdrstop 不能在 #if block 内
为了防止上述错误,我将宏条件
语句移植到stdafx.h
预编译中,如下所示:
******* stdafx.h ********
#pragma once
#if defined (_MSC_VER)
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN
// Windows Header Files:
#include <windows.h>
#endif
**************************
这是避免上述预编译单元编译错误的好方法吗?
最佳答案
这种方法行不通。在编译过程中忽略预编译 header 包含之前的所有内容,因此 #if defined (_MSC_VER)
in dummy.cpp
将被丢弃。
更好的方法是删除所有#include "stdafx.h"
并通过编译选项Forced Include File
包含预编译 header :
/FI"stdafx.h"
关于c++ - 在预编译单元中定义宏条件语句是否明智?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48986712/