This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

C252 and #if statements

I have a header file that will be compiled by multiple different compilers. This being the case, I want to use #if statements to change the compilation flow based on the current compiler. In the Keil compiler case, I want to use the #pragma NOIV to disable vector table generation, so I used the following at the beginning of the file:

#if(__C51__)

/* some comment */
#pragma NOIV

#endif /* end __C51__ */

I received the C252 error about misplaced control lines. Looking at the documentation, blank lines and comment lines should be ignored, so I would expect what I have written to work, since the pre-processor should remove or add the pragma before the compiler is invoked. Is this case, or is it impossible to use control lines with #if statements?

Thanks, Ian.

Parents
  • With regards to the blank line/comment versus not one of those, if I had done the correct thing, which is use a #ifdef, instead of a #if, I would expect it to work, regardless of documentation, because #ifdef statements really should be stripped by the pre-processor, rather than have the compiler deal with them and trip on them.

    As for your more general solution, I agree, and will pursue that avenue rather than the current one. Thanks.

    -Ian

Reply
  • With regards to the blank line/comment versus not one of those, if I had done the correct thing, which is use a #ifdef, instead of a #if, I would expect it to work, regardless of documentation, because #ifdef statements really should be stripped by the pre-processor, rather than have the compiler deal with them and trip on them.

    As for your more general solution, I agree, and will pursue that avenue rather than the current one. Thanks.

    -Ian

Children
  • #ifdef/#ifndef has been deprecated ever since ANSI C89. The preferred syntax these days is

    #if defined(MYSYMBOL)

    or

    #if !defined(MYSYMBOL)

  • that's why good editors have "global replace" :)

    I routinely use #ifdef/#ifndef simply because a ton of old inherited still maintained code does. Having 2 forms will just be waaaaaaaaay too confoosing (does this use 'modern', or does this use 'old') which will, gradually, lead to a mix in virtually every thing we have.

    Of course, the day Keil stop supporting #ifdef, every project touched will have, at that time to go through a global replace process.

    Erik

  • Oh, there's WAY too much code out there with #ifdef for any compiler ever to stop supporting it. It'll just remain deprecated forever :)

    I try to write new code the new way, though. ("New", as if 1989 were new... There are people writing code younger than the preprocessor statement.)

    In general I like to avoid #if defined() at all. Usually I resolve alternate implementations at link time. That is, I just have two files with two implementations and link in the right one, rather than having one file that's been turned into hash with #if. Or in the case of headers, I just use the header with definitions appropriate for the platform, rather than chaining through every possibility.

    But there's always some bit of code that's just too small to justify pulling out from a module that otherwise identical. #ifdef will never die.