#ifdef does not work as expected with precompiled headers

I have a piece of code as shown below:

#define FEATURE_A 1 void function() { // some code #ifdef FEATURE_A // code to be executed when this feature is defined #endif // some code } 

The program does not execute code inside #ifdef - #endif . But when I change #ifdef to #ifndef and delete the #define macro, the code runs. The code below works as expected.

 //#define FEATURE_A 1 void function() { // some code #ifndef FEATURE_A // code to be executed when this feature is defined #endif // some code } 

Can someone explain why in the first case the code inside #ifdef - #endif is not executed, and in the second case it works? Can someone tell me which setting might be wrong?

Not sure if I am using visual studio 2010 in this case.

Thanks in advance

UPDATE: When I clean and start up again, the second one doesn't work either. his only show in the editor as code included.

When I define the macro in the project-> property-> Configuration Properties-> c / C ++ β†’ Preprocessor file, both of them work fine.

+7
c ++ macros
source share
1 answer

Probably because of how Microsoft implements precompiled headers. Do you really have

 #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 } 

Move it after the precompiled header and everything works:

 #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 } 
+10
source share

All Articles