Functional macro definition from command line

Functional macro definition from command line

Hi, I would like to define a series of functional macros intended to be used in several source files, but without altering said source files.

The first thing which came to my mind was to use -D, but I was unable to find any hint if -D can be used to define functional macros. For example: ifort -cpp -D'f(x,y)=x*y' which should be equivalent to #define f(x,y) x*y like it is in gfortran.

The alternative approach would be to use a command line option to include a header file containing all the definitnions, so I could compile multiple files with, say ifort -cpp --header-file=my_header.h.

Is any of those two approaches possible with ifort? Are there any alternatives which won't require the use of #include "my_header.h" in every source file?

Best regards,

Paweł Biernat.

7 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

The ifort pre-processor which is somewhat compatible with cpp syntax is activated by the -fpp option. This option also supports a file inclusion like C header files.

Still, I don't get the expected behavior. Calling

ifort -fpp -D'f(x,y)=x*y' test.f90

Results in:
/tmp/ifortjmesyi.i90: catastrophic error: Internal Compiler Error: Bad syntax, '=' expected while processing '@f(x,y)='x*y'''
compilation aborted for test.f90 (code 1)

The same happens with -cpp.

The code in test.f90 is as simple as it can be:

program test
print *, f(3,2)
end program test

edit: ifort version: 13.0.0 20120731

Paweł Biernat.

Does it work without the ' ' ?
By the way, ifort is frequently used with the gcc pre-processor, e.g.
gfortran -E test.F90 > test.f90
ifort test.f90
Of course, in the simplest cases fpp should give the same result as gfortran -E test.F90 or gcc -E -traditional -x c test.F90, either of which invoke tradcpp (no C99 or C++ syntax interpretations which will break Fortran source).

Omitting quotes results in another error:

$ ifort -fpp -Df(x,y)=x*y test.f90
/tmp/ifortE8QPVI.o: In function `MAIN__':
test.f90:(.text+0x3f): undefined reference to `f_'

Looks like the macro was disregarded. I also tried using the quotes for the rhs only as in -Df(x,y)='x*y', the result is the same, either way it doesn't work.

As a last resort I will use gfortran because preprocessing multiple files in place is not an option in my case.

Could you possibly point me the option which allows to include header files from the command line?

Paweł Biernat.

Compile with FPP, however, place into your .f90 file

#include "YourMacroFileHere.h"

Note the # preceeding the include is an FPP directive.
INCLUDE without a # is a ifort FORTRAN directive.

Unlike the fortran INCLUDE which is scoped to a subroutine or COMMON or MODULE header,

Should #include contain #define they are not scoped, rather they live from the #define through end of file or #undef

I tend to compile always with fpp and insert

! MyProgram.f90 - some comment here
! more comments

#include "MyDefinesHere.h"
subroutine Foo(...

Jim Dempsey

Thanks, "#include" should definitely work, but I am interested in the command line version of "#include" which would leave the .f90 files intact but still use the macros from the header file. Something which would replace multiple "-D" options, although I would be completely content with "-D" working for function-like macros.

To complete the example in original post, here is the expected output produced by gfortran 4.7.1 20120721 (prerelease):

$ gfortran -cpp -D'f(x,y)=x*y' test.f90 && ./a.out

Here apostrophes are mandatory, nevertheless "-D" works as expected.

Best regards,
Paweł Biernat.

Leave a Comment

Please sign in to add a comment. Not a member? Join today