C++ #ifdef在预编译标头中未按预期工作

C++ #ifdef在预编译标头中未按预期工作,c++,macros,C++,Macros,我有如下部分代码: #define FEATURE_A 1 void function() { // some code #ifdef FEATURE_A // code to be executed when this feature is defined #endif // some code } 程序不执行#ifdef-#endif中的代码。但当我将#ifdef更改为#ifndef并删除#define宏时,代码就会执行。下面的代码按预期工作 //#define FEATU

我有如下部分代码:

#define FEATURE_A 1

void function()
{
// some code

#ifdef FEATURE_A
    // code to be executed when this feature is defined
#endif

// some code

}
程序不执行#ifdef-#endif中的代码。但当我将#ifdef更改为#ifndef并删除#define宏时,代码就会执行。下面的代码按预期工作

//#define FEATURE_A 1

void function()
{
// some code

#ifndef FEATURE_A
    // code to be executed when this feature is defined
#endif

// some code

}
有人能解释为什么在第一种情况下,#ifdef-#endif中的代码没有执行,而在第二种情况下它可以工作吗?有人能告诉我什么设置可能出错吗

不确定这是否重要,我正在使用VisualStudio2010

提前谢谢

更新: 当我清理并重新运行时,第二个也不起作用。它在编辑器中仅显示为启用的代码


当我在project->property->Configuration Properties->c/c++->Preprocessor中定义宏时,它们都可以正常工作。

这可能是因为Microsoft如何实现预编译头。你真的有

#define FEATURE_A 1
#include "stdafx.h" // <- all code even ascii art before that line is ignored.

void function()
{
// some code

#ifdef FEATURE_A
    // code to be executed when this feature is defined
#endif

// some code

}
#定义特征1

#包括“stdafx.h”//你确定你没有
#undef FEATURE_A
吗?如果你想在预处理后看到代码,请使用
-E
标记编译,你需要发布一个显示此行为的整个程序,因为否则没有任何意义。是的,我确定。我刚才已经检查过了。我记得我在Visual Studio中遇到过很多问题,有时候帮助我的是清除它为项目保留的对象文件。试试看,也许会有帮助
#include "stdafx.h" // <- all code even ascii art before that line is ignored.
#define FEATURE_A 1

void function()
{
// some code

#ifdef FEATURE_A
    // code to be executed when this feature is defined
#endif

// some code

}