[comp.lang.fortran] Using cpp, m4 for macro substitution in Fortran. Summary.

quinlan@physics.utoronto.ca (Gerald Quinlan) (07/08/90)

Thanks to the people who responded to my question about using cpp to
perform macro substitutions in Fortran programs.  Nobody knew how to get
cpp to do what I wanted it to do (it probably isn't possible).  However,
Sergio Gelato showed me how to do it with the m4 macro program.  A trivial
example of how this works is shown below.  The Makefile says that "swap.f"
is made from "swap.F" by running "swap.F" first through cpp and then
through m4.  I suppose I could eliminate the cpp stuff and do everything
with m4, but the program that I wanted to use this trick in has a bunch of
cpp commands and I'm too lazy to change them.


----------------------------- Makefile -----------------------------------
# Tell make how to make .f files from .F files.
.SUFFIXES: .F .f
.F.f:
	/lib/cpp -P $(CPPFLAGS) $*.F  | m4 > $*.f

----------------------------- swap.F  -----------------------------------

#ifdef INLINE_SWAP
define(CALL_SWAP,`dummy=$1
      $1=$2
      $2=dummy')
#endif
      program swap
      a=3.0
      b=4.0
#ifdef INLINE_SWAP
      CALL_SWAP(A,B)
#else
      call swap(a,b)
#endif
      stop
      end

------------------------ make swap.f ------------------------------------



      program swap
      a=3.0
      b=4.0

      dummy=A
      A=B
      B=dummy

      stop
      end

--------------- make "CPPFLAGS=-DINLINE_SWAP" swap.f ------------------------



      program swap
      a=3.0
      b=4.0

      dummy=A
      A=B
      B=dummy

      stop
      end

-----------------------------------------------------------------------------