user1628622
user1628622

Reputation:

Fortran #define macro definitions with multiple lines

I was under the impression C-style macro definitions work in gfortran?

#define ERROR_CHECKER(msg,stat) \
      IF (stat.NE.0) THEN \
         DO i = 1,BIG \
            IF(msg(i).NE.C_NULL_CHAR)THEN \
               ErrMsg(i:i) = msg(i) \
            ELSE \
               EXIT \
            END IF \
         END DO\
         IF(stat.EQ.1) THEN \
            ErrStat = ID_Warn \
         ELSE \
            ErrStat = ID_Fatal \
            RETURN \
        END IF \
     END IF

But then this error ruins my day:

IF (stat.NE.0) THEN      DO i = 1,BIG         IF(message
               1
Error: Cannot assign to a named constant at (1)

Any ideas what I am doing wrong here?

Secondary question: does intel fortran recognize c-style macros? If so, are compiler flags necessary?

Upvotes: 3

Views: 2519

Answers (2)

cup
cup

Reputation: 8267

Instead of using a macro, just convert the macro to a function. That way you don't have a massive dependency on the Fortran compiler having a macro facility

LOGICAL FUNCTION ERROR_CHECKER(msg,stat)
  character*(*) msg(*)
  integer stat
  IF (stat.NE.0) THEN
     DO i = 1,BIG
        IF(msg(i).NE.C_NULL_CHAR)THEN
           ErrMsg(i:i) = msg(i)
        ELSE
           EXIT
        END IF
     END DO
     IF(stat.EQ.1) THEN
        ErrStat = ID_Warn
     ELSE
        ErrStat = ID_Fatal
        RETURN .FALSE.
    END IF
 END IF
 RETURN .TRUE.
 END FUNCTION

In your code

IF (ERROR_CHECKER(msg, stat)) RETURN

Edit: Some of the later versions of Fortran have statement separators (;) which can be used. Unfortunately, the line length is limited so your macro cannot be very long, nor can it contain more than one control structure.

Upvotes: 1

Raul Laasner
Raul Laasner

Reputation: 1585

Aside from the fact that such a macro approach is not good style, you are missing the necessary line breaks in the generated code. This can be fixed by putting a semicolon before each backslash in the macro definition (except the first line).

Upvotes: 0

Related Questions