Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

conditional compiling

wenbinyu
Novice
1,309 Views

I am interested in using conditional compiling using ifort. I have the following simple code with name test.f90

program ConditionalCompiling
implicit none

#IFDEF Flag
write(*,*)"Compiled with Flag defined"
#ELIF
write(*,*)"Not Compiled with Flag defined or compiled with Flag not defined"
#ENDIF
end program ConditionalCompiling

However, when I use ifort -c test.f90. It gives me the following message. Can you help me to get it compiled and behave as expected? Thanks a lot!

Intel(R) Visual Fortran Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 12.1.5.344 Build 20120612
Copyright (C) 1985-2012 Intel Corporation. All rights reserved.

test.F90(4): warning #5117: Bad # preprocessor line
#IFDEF Flag
-^
test.F90(6): warning #5117: Bad # preprocessor line
#ELIF
-^
test.F90(8): warning #5117: Bad # preprocessor line
#ENDIF
-^

0 Kudos
8 Replies
Steve_Lionel
Honored Contributor III
1,304 Views

Add the -fpp switch.

0 Kudos
wenbinyu
Novice
1,299 Views

Steve, thanks a lot for your quick response. When I use 

ifort -c -fpp test.f90

I got:

test.f90(6): #error: #if: parse error.

0 Kudos
mecej4
Honored Contributor III
1,283 Views

Your program contains the directive #ELIF instead of #ELSE. 

#IFDEF <symbol>

..

#ELIF <condition>

..

#ENDIF

versus

#IFDEF <symbol>

..

#ELSE

..

#ENDIF

wenbinyu
Novice
1,273 Views

Yes, this fixes the issue. Thanks a lot! Now I have another question. If defined flag, I want to include file1.f90, if not defined include file2.f90.  How can this be done? I tried the following. It does not work. 
#IFDEF Flag
include 'file1.f90'

#else

include 'file2.f90'

#ENDIF 

0 Kudos
mecej4
Honored Contributor III
1,262 Views

Did you remember to use the /fpp or -fpp option?

 

Simply saying "It does not work" is not helpful. You have to describe the command that you used, provide information regarding the contents of the source file, and display the error messages that you saw.

0 Kudos
wenbinyu
Novice
1,252 Views

Attached are three files. When I compile using

ifort -fpp test.f90 -o test

I execute test and obtain 
 15.00000
Not Compiled with Flag defined or compiled with Flag not defined

When I compile using 

ifort -DFlag -fpp test.f90 -o test

I execute test and obtain 
15.00000
Compiled with Flag defined

I was expecting the second number to be 200.00000

0 Kudos
mecej4
Honored Contributor III
1,246 Views

Preprocessors (originating in relation to C rather than Fortran) are usually case sensitive. Make sure that your preprocessor macros such as #ifdef are lowercase, and that macro arguments such as FLAG, etc.,  are uppercase (or at least consistently cased). You spelled "include" as "inlcude" in one place. 

 

0 Kudos
wenbinyu
Novice
1,239 Views

Thanks a lot. After fixing a few typos, now it works.

Reply