How to define a variable in a makefile and then use it within Fortran code

会有一股神秘感。 提交于 2019-12-11 02:59:37

问题


I am trying to define a variable in a makefile, and then depending on whether that variable is set, change which code block is compiled in my Fortran routine.

Simple example I can't get working:

program test
    implicit none
    integer :: a
#ifdef MYVAR
    a = 1
#else
    a = 0
#endif
    write(*,*) a
end program test

My makefile is:

MYVAR=1
all:
    ifort temp.F90 -fpp
    echo $(MYVAR)

The echo $(MYVAR) line correctly prints 1. However, when the test program is compiled it sets a=0. How do I get the Fortran code to recognize MYVAR?


回答1:


You need to add an extra flag

OPTIONS = -DMYVAR=$(MYVAR)

and then you compile it with

all:
    ifort $(OPTIONS) <file.f90> -fpp

And you should be good to go.




回答2:


You need to specify the variable at compile time (in your Makefile) using -D:

all:
    ifort -DMYVAR=1 temp.F90 -fpp
    echo $(MYVAR)

Or, since you just check whether it is defined or not:

all:
    ifort -DMYVAR temp.F90 -fpp
    echo $(MYVAR)


来源:https://stackoverflow.com/questions/22512001/how-to-define-a-variable-in-a-makefile-and-then-use-it-within-fortran-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!