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

Preprocessing issue with VS

Yaqi_Wang
Beginner
901 Views
To demonstrate this issue, I created three files.

The first 'dim.h' is a one-line header file with a variable definition in it:

#define dim 2

The second file 'typ_boundary.f90' contains a module definition:

#include "dim.h"
#if (dim==2)
MODULE TypBoundary_2D
#elif (dim==3)
MODULE TypBoundary_3D
#endif

INTEGER, PARAMETER :: vacuum_bc =-1
INTEGER, PARAMETER :: reflecting_bc =-2
INTEGER, PARAMETER :: MaxNonHom_bc =-10
INTEGER, PARAMETER :: MinNonHom_bc =-99

! boundary types
INTEGER, PARAMETER, PRIVATE :: num_bdry_types = 2

INTEGER, PARAMETER :: vacuum = 1
INTEGER, PARAMETER :: reflecting = 2

CONTAINS

FUNCTION boundary_type(boundary_indicator)
IMPLICIT NONE
INTEGER :: boundary_indicator, boundary_type
SELECT CASE (boundary_indicator)
CASE (vacuum_bc, MinNonHom_bc:MaxNonHom_bc)
boundary_type = vacuum
CASE (reflecting_bc)
boundary_type = reflecting
CASE DEFAULT
stop 'wrong indicator'
END SELECT
RETURN
END FUNCTION boundary_type

LOGICAL FUNCTION is_boundary(boundary_indicator)
IMPLICIT NONE
INTEGER :: boundary_indicator
SELECT CASE (boundary_indicator)
CASE (vacuum_bc, MinNonHom_bc:MaxNonHom_bc, reflecting_bc)
is_boundary = .true.
CASE DEFAULT
is_boundary = .false.
END SELECT
RETURN
END FUNCTION is_boundary

INTEGER FUNCTION num_boundary_type()
IMPLICIT NONE
num_boundary_type = num_bdry_types + 1
RETURN
END FUNCTION num_boundary_type

INTEGER FUNCTION periodic_type()
IMPLICIT NONE
periodic_type = num_boundary_type()
RETURN
END FUNCTION periodic_type

#if (dim==2)
END MODULE TypBoundary_2D
#elif (dim==3)
END MODULE TypBoundary_3D
#endif

The last file 'Source1.f90' is the main:

#include "dim.h"
program test
#if (dim==2)
USE TypBoundary_2D
#elif (dim==3)
USE TypBoundary_3D
#endif
implicit none

print *, num_boundary_type()

stop
end program test

We can compile these three files with Visual Studio+Intel Visual
Fortran by turning fpp (fortran preprocess for all derivatives) on and
adding the directory where dim.h is in into the including directory.
I am using Intel Visual Fortran Compiler Integration for Microsoft
Visual Studio* 2008, 11.1.3471.2008.
The problem is that Visual Studio always do the rebuild even nothing
is changed after the last build.

I also create a simple makefile with gfortran in cygwin:

testpre : typ_boundary.o Source1.o
gfortran -o testpre typ_boundary.o Source1.o

typ_boundary.o : typ_boundary.f90 dim.h
gfortran -c -cpp -I. typ_boundary.f90

Source1.o : Source1.f90 dim.h typ_boundary.o
gfortran -c -cpp -I. Source1.f90

It just works fine.

So it must be the preprocessing derivatives that cause VS always do
the rebuild. Is this a bug in VS or something I misunderstand?

Thanks.

0 Kudos
5 Replies
Yaqi_Wang
Beginner
901 Views
found an easy solution:
instead using,
#if (dim==2)
MODULE TypBoundary_2D
#elif (dim==3)
MODULE TypBoundary_3D
#endif
use,
MODULE &
#if (dim==2)
TypBoundary_2D
#elif (dim==3)
TypBoundary_3D
#endif
0 Kudos
Yaqi_Wang
Beginner
901 Views
Apparently, module keyword and the module name must be on the same line so that VS can create correct dependencies.
So the solution in my previous post is incorrect.
0 Kudos
jimdempseyatthecove
Honored Contributor III
901 Views
Try:


#if (dim==2)
#define TypBoundary_nD TypBoundary_2D
#elif (dim==3)
#define TypBoundary_nD TypBoundary_3D
#else
#error error in dim
#endif
MODULE TypBoundary_nD

Jim Dempsey
0 Kudos
Yaqi_Wang
Beginner
901 Views
VS still do the rebuild every time.
But this does help on cleaning up my code. Thanks.
0 Kudos
jimdempseyatthecove
Honored Contributor III
901 Views
Try placing your modules into a seperate project (static library)from the program/subroutines/functions.

Note, if possible, have the module library contain all the varients of the modules xxx_2D, xxx_3D, ...

The output of the module library project will be

YourModuleLibrary.lib
plus
xxx_2D.MOD
xxx_3D.MOD
...

Make the program/subroutines/functions project dependent on the modules project. (and set build order accordingly)

These projects can all be in the same solution.

Jim Dempsey
0 Kudos
Reply