#if statements and comments in pgcc

Hi,

pgcc gives errors and refuses to compile when I use #if statements like the following:

#if (CONDITION)
codeToRun();
#endif

Also it doesn’t seem to accept c++ style // comments.

Is it possible to make it accept either of these?

david

Hi David,

Can you post a sample code which gives the error? Below is a test program which shows various methods of using conditional preprocessor directives, including “#if (CONDITIONAL)”. Note that you can’t use parentheses after “#ifdef/#ifndef”.

C++ style-comments are not part of the K&R or C89 standards, so are not allowed by default. However, adding the “-B” flag tells the compiler to allow C++ comments. The C99 standard does allow C++ comments, so using the “-c9x” flag also works.

  • Mat
% cat prepro.c
#include <stdio.h>

// This is a C++ comment

int main () {

#if (MY_DEF == 2)
  printf("MY_DEF == 2\n");
#endif

#if MY_DEF > 2
  printf("MY_DEF > 2\n");
#endif

#if (MY_DEF)
  printf("MY_DEF is %d\n", MY_DEF);
#endif

#if (defined(MY_DEF))
  printf("MY_DEF is %d\n", MY_DEF);
#endif

#ifdef MY_DEF
  printf("MY_DEF is %d\n", MY_DEF);
#endif

#ifndef MY_DEF
  printf("MY_DEF is not defined\n");
#endif

}
% pgcc -DMY_DEF prepro.c
PGC-S-0030-Syntax error: Malformed function or data declaration at '/' (prepro.c: 3)
PGC-S-0029-Syntax error: Recovery attempted by deleting from '/' on line 3 through identifier comment on line 3 (prepro.c)
PGC/x86-64 Linux/x86-64 Rel Dev: compilation completed with severe errors
% pgcc -DMY_DEF -B prepro.c
% a.out
MY_DEF is 1
MY_DEF is 1
MY_DEF is 1
% pgcc -DMY_DEF=10 -c9x prepro.c
% a.out
MY_DEF > 2
MY_DEF is 10
MY_DEF is 10
MY_DEF is 10

Thanks! There were no problems after I added the -B flag; it must have been giving the error on the #if statements because it was confused by the comments.

cheers
dave