Menu

Compile Error with assertions

Ben
2008-09-03
2012-09-26
  • Ben

    Ben - 2008-09-03

    Hi, I've tried to compile quite a large C program. The problem is at every instance of an assert statement in my code I get a compile error stating "syntax error at '#' token". For example assert (stack == 1); gives this error message. I have sent this exact code to another person and they claim that it compiles fine on their system (this person is not using Dev C++). I am therefore wondering if this is a known issue with Dev-C++?
    Thanks for any feedback.
    -B

     
    • Ben

      Ben - 2008-09-03

      I also feel I should point out that the only place my code contains a '#' character is right at the start where #include and #define have been used.

       
    • Wayne Keen

      Wayne Keen - 2008-09-03

      A "simple as possible example" program is really called for here, something that compiles and shows the error, like a simple "Hello World" with an added assertion enough to show how you are implementing things.

      Please also post your Basic 3 for this example. They are covered in the thread titled "Please Read Before Posting a Question" (Did you read that before posting?)

      Wayne

      p.s. Have you compiled simple code already? I ask this as a surprising number of people go right from download to compiling complex code.

       
    • cpns

      cpns - 2008-09-03

      > I should point out that the only place my code contains
      > a '#' character is right at the start where #include

      That may be so, but what did you #include? My guess is that ypu have included a file that defines assert() as a macro, and that that macro is incorrectly defined; probably an incorrect attempt at 'stringification'. We need to see the definition of assert(). This is a good example of why macros are a bad idea -they cause confusing error messages.

      If you still cannot see it, to demonstrate that my diagnosis is correct enter this in your code just after the #includes:

      include <stdio.h> // not needed if you already have it.

      include <stdlib.h> // ditto.

      undef assert

      define assert( b ) do{ if(!b){ printf( "\nassert( %s ) failed at %s:%d\n", #b, FILE, LINE ); exit(-1);} }while(0) ;

      Do not attempt to split this last line up, it must be on one line - even if the forum line wraps it. (Another reason to avoid macros!).

      If that works you have a badly defined assert in one of your #include files, or a nested include within them. You might compare the definition with mine.

      Clifford

       
    • Ben

      Ben - 2008-09-04

      Hi, thanks for the replies. I apologize for not posting the big 3 but here it is better late than never:

      Version 4.9.9.2
      OS: Windows XP.

      Some compact sample code which demonstrates my problem (I have used Clifford's advice but unfortunately it still produces the same error whether or not I include assert.h or use his definition of assert):

      undef assert

      define assert( b ) do{ if(!b){ printf( "\nassert( %s ) failed at %s:%d\n", #b, FILE, LINE ); exit(-1);} }while(0) ;

      include <stdio.h>

      int main() {

      int  i = 0;
      
      do {
            assert (i++ &lt; 250); 
            printf(&quot;looping&quot;);
            i++;
      }
      while (i &lt; 5);
      

      }

      The corresponding compiler log for this code:

      D:\gp\Untitled1.c In function `main':
      9 D:\gp\Untitled1.c syntax error at '#' token

      Line 9 corresponds to the assert statement below do {.
      Also I have wrote a couple of programs with Dev C++ without any problems, however this is the first time I have ever used assert.
      Thanks for any help
      -B

       
    • cpns

      cpns - 2008-09-04

      Aaaaaarrrrrrrggggggghhhhhhhh! It does not help if you do not follow my instructions.

      I explicitly said "enter this in your code just after the #includes". You placed the code before <stdio.h>. The chances are that <stdio.h> indirectly includes <assert.h> and this redefines the macro.

      > The corresponding compiler log for this code:

      No it is not. The text from the "Compile Log" tab, not the "Compiler" tab. The latter filters out a lot of useful diagnostic information. The "Compile Log" is plain text and can easily be copy and pasted in its entirety (right click in the text).

      I also said "We need to see the definition of assert()". I do not have Dev-C++ installed, so I cannot look, and it appears that yours may have been modified in any case. The possibilities are either that you are picking up the assert.h from a different compiler, or that yours has been modified. The compile log will provide information that may indicate the first, and you will actually have to look in assert.h to show us the second.

      It is also possible I suppose that the build is being performed with an old version of the C pre-processor that does not support stringification. Do you have other GNU toolchain installations other than the Dev-C++ MinGW installation?

      Cliffprd

       

Log in to post a comment.