9

我有如下代码的一部分:

#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中的代码没有执行,而在第二种情况下它可以工作?谁能告诉我什么设置可能是错误的?

不确定这是否重要,我使用的是 Visual Studio 2010。

提前致谢

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

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

4

2 回答 2

15

这可能是因为 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

}

在预编译头文件和所有工作之后移动它:

#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

}
于 2013-07-31T05:25:53.610 回答
0

就我而言,我有多个Platform,所以我需要Preprocessor为每个Platform.

在此处输入图像描述

于 2021-07-09T10:59:21.117 回答