Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

One fortran file to be compiled in different ways

Status
Not open for further replies.

thisgonnabemyname

Programmer
May 11, 2010
2
Hey there,

I have a programm (which consists of one fortran file, let's assume it bla.f). I added some functionalities to it (it's a code-block which is completely independend from the others), which needed external libraries (so I made use of the "use MODULE_XYZ" statement in the fortran file).

my question is the following: is there any possibility to have only ONE bla.f file and compile it in two different versions:

1. "make": compile the program as if the added code wouldn't exist in the file (I don't know how to "disable" certain lines of code therefor)
2. "make bla": compile the program in the latest version, which means: compile it including the new code.

Is this possible? Would be nice for checking it into the CVS repository without having two versions of code in bla.f, which was there before I added functionality.

thanks so far!
 
What you need is conditional compilation.
In gfortran and g95 we can use C-preprocessor for conditional compilation - exaple:
cnd_cmp.f95
Code:
[COLOR=#0000ff]! Conditional Compilation in gfortran and g95[/color]
[COLOR=#0000ff]! Either define directive inside of the program (in 1. column of a line) as[/color]
[COLOR=#0000ff]!   #define BLAH[/color]
[COLOR=#0000ff]! and compile with[/color]
[COLOR=#0000ff]!   gfortran -x f95-cpp-input cnd_cmp.f95 -o cnd_cmp[/color]
[COLOR=#0000ff]!   g95 -cpp cnd_cmp.f95 -o cnd_cmp[/color]
[COLOR=#0000ff]! or define directive outside of the program as a command line switch[/color]
[COLOR=#0000ff]!   gfortran -x f95-cpp-input -DBLAH cnd_cmp.f95 -o cnd_cmp[/color]
[COLOR=#0000ff]!   g95 -cpp -DBLAH cnd_cmp.f95 -o cnd_cmp[/color]
[COLOR=#a020f0]program[/color] cnd_cmp
[COLOR=#a020f0]#ifdef BLAH[/color]
  [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'This is BLAH branch of code'[/color]
[COLOR=#a020f0]#else[/color]
  [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'This is other code ..'[/color]
[COLOR=#a020f0]#endif[/color]
[COLOR=#a020f0]end program[/color] cnd_cmp
Output:
Code:
$ gfortran -x f95-cpp-input cnd_cmp.f95 -o cnd_cmp
$ cnd_cmp
 This is other code ..
$ gfortran -x f95-cpp-input -DBLAH cnd_cmp.f95 -o cnd_cmp
$ cnd_cmp
 This is BLAH branch of code

$ g95 -cpp cnd_cmp.f95 -o cnd_cmp
$ cnd_cmp
 This is other code ..
$ g95 -cpp -DBLAH cnd_cmp.f95 -o cnd_cmp
$ cnd_cmp
 This is BLAH branch of code
If you define the directive inside of your source
Code:
[COLOR=#a020f0]#define BLAH[/color]
you can compile without option -D, i.e.
Code:
$ gfortran -x f95-cpp-input cnd_cmp.f95 -o cnd_cmp
or
Code:
$ g95 -cpp cnd_cmp.f95 -o cnd_cmp
and you will get the result
Code:
$ cnd_cmp
 This is BLAH branch of code

This was how to do it in gfortran and g95. I have no knowledge about if other compilers support conditional compilation or not.
 
Ahh, very nice. Thank you very much. Now I know what to look after ;) I think we're using Intels Fortran Compiler :)
 
1) Pop up the compiler documentation.
2) Look for [bold]IF directive[/bold]
3) The first entry tells you everything you need to know, including an example.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top