-1

For some reason I need to define some nested definition. but the pre-processor remove a part of my macro. Look at following code:

#define SINGLE_ITERATION y+=xext[++xIndex]*H[hCounter--];
#define LOOP_ITERATION  SINGLE_ITERATION  \
    SINGLE_ITERATION SINGLE_ITERATION \
    SINGLE_ITERATION SINGLE_ITERATION \
    SINGLE_ITERATION SINGLE_ITERATION \
    SINGLE_ITERATION

#define DEBUG_CODE #ifdef MEMORIZE \
    myOutput[xCounter]=y;      \
    #endif                     \
    #ifdef STORING            \
    fprintf(fid,"%f\r\n",y);  \
    #endif

#define OUT_LOP_ITERATION y=0;           \ 
                          xIndex=xCounter-HL;\
                          for(hCounter=HL_MINUS_1;hCounter>0;)
                          {LOOP_ITERATION}  \
                          SINGLE_ITERATION  

But the problem is when the MEMORIZE flag is defined. Here is the output of pre-processor created by compiler

for( xCounter=XL_MINUS_1;xCounter>=0;xCounter--)
{
y=0; 
xIndex=xCounter-HL;
for(hCounter=HL_MINUS_1;hCounter>0;)
{
    y+=xext[++xIndex]*H[hCounter--];
    y+=xext[++xIndex]*H[hCounter--];
    y+=xext[++xIndex]*H[hCounter--];
    y+=xext[++xIndex]*H[hCounter--];
    y+=xext[++xIndex]*H[hCounter--];
    y+=xext[++xIndex]*H[hCounter--];
    y+=xext[++xIndex]*H[hCounter--];
    y+=xext[++xIndex]*H[hCounter--];
    }
    y+=xext[++xIndex]*H[hCounter--];
#ifdef  
myOutput[xCounter]=y;
#endif 
#ifdef STORING 
fprintf(fid,"%f\r\n",y); 
#endif
}

which has a problem in the last part.in the :

#ifdef  
myOutput[xCounter]=y;
#endif 
#ifdef STORING 
fprintf(fid,"%f\r\n",y); 
#endif
}

which must be

#ifdef  MEMORIZE
myOutput[xCounter]=y;
#endif 
#ifdef STORING 
fprintf(fid,"%f\r\n",y); 
#endif
}

and the pre-processor omitted the MEMORIZE because it is defined currently.

Please help me to solve it.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
MrDoDo
  • 35
  • 2
  • 5
  • 2
    This set of macros will make your code so hard to debug in the future. Do consider refactoring without using macros at all. – Bathsheba Feb 14 '14 at 06:28
  • 1
    You can't put #ifdef in a #define like that. Duplicate of http://stackoverflow.com/a/5586469/2958164 – Phillip Kinkade Feb 14 '14 at 06:28
  • Preprocessor macros don't nest cleanly. – seand Feb 14 '14 at 06:31
  • You cannot `#define` pre-processor directives. However, if you do, they won't get pre-processed as you are observing. – alk Feb 14 '14 at 06:58
  • I'm sorry, but this program needs to be rewritten from scratch. Looking at the code posted, it seems highly unlikely that you will be able to salvage any bug free code from it. – Lundin Feb 14 '14 at 07:11

2 Answers2

0

You probably wanted:

#ifdef MEMORIZE 
#define DEBUG_CODE myOutput[xCounter]=y;
#endif

#ifdef STORING
#define DEBUG_CODE fprintf(fid,"%f\r\n",y);
#endif
Marian
  • 7,402
  • 2
  • 22
  • 34
0

You may try this:

#if defined(MEMORIZE) && defined(STORING)
#define DEBUG_CODE myOutput[xCounter]=y;\
                                        fprintf(fid,"%f\r\n",y);
#elif defined(MEMORIZE)
#define DEBUG_CODE myOutput[xCounter]=y;
#elif defined(STORING)
#define DEBUG_CODE fprintf(fid,"%f\r\n",y);
#else
#define DEBUG_CODE
#endif
Jerry_Y
  • 1,724
  • 12
  • 9