[英]Is it wise to define macro conditional statement within Precompilation unit?
******* 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 ++编译器弹出以下编译器错误:
error C2856: #pragma hdrstop cannot be inside an #if block
为了防止出现上述错误,我将macro conditional
语句移植到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
**************************
这是避免上述预编译单元编译错误的好方法吗?
这种方法不起作用。 预编译头包含之前一切都在编译过程中被忽略,以便#if defined (_MSC_VER)
在dummy.cpp
将被扔掉。
更好的方法是删除所有#include "stdafx.h"
并通过编译选项包含预编译头Forced Include File
:
/FI"stdafx.h"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.