Two modules with the same name in one program

Here is a very simple example of duplicate modules with the same name. These 5 files go into the same directory.

! File t_duplicate_modules.f90

PROGRAM t_duplicate_modules

	USE m_duplicate

	WRITE(*,'("In PROGRAM t_duplicate_modules")')
	WRITE(*,*)module_version

	CALL s_duplicate_modules

END PROGRAM t_duplicate_modules


! File s_duplicate_modules.f90

SUBROUTINE s_duplicate_modules

	USE m_duplicate

	WRITE(*,'(/,"In SUBROUTINE s_duplicate_modules")')
	WRITE(*,*)module_version

END SUBROUTINE s_duplicate_modules


! File: m_duplicate_1.f90

MODULE m_duplicate

	CHARACTER(LEN=*),PARAMETER :: module_version = "Version 1"

END MODULE m_duplicate


! File: m_duplicate_2.f90

MODULE m_duplicate

	CHARACTER(LEN=*),PARAMETER :: module_version = "Version 2"

END MODULE m_duplicate


#!/bin/csh

# File: build.csh

gfortran -c m_duplicate_1.f90
gfortran -c t_duplicate_modules.f90

gfortran -c m_duplicate_2.f90
gfortran -c s_duplicate_modules.f90

gfortran -o t_duplicate.exe *.o

# End of build.csh


And when we run it we get:

john@gemsbok:~/projects/WinFPT/fpt/fpttest/duplicate_modules$ ./build.csh
john@gemsbok:~/projects/WinFPT/fpt/fpttest/duplicate_modules$ ./t_duplicate.exe
In PROGRAM t_duplicate_modules
 Version 1

In SUBROUTINE s_duplicate_modules
 Version 2

So we have 2 different modules with the same name in the same directory, and the compiler and linker can’t know.

As I posted earlier, this actually happened because modules with the same name were used to build 4 different libraries in the same project. I need a solution to this situation very soon, so what I propose for IDEs and analysis tools (at least for ours) is:

  1. We define “module domains” and put all primary Fortran files into domains.

ii. All top-level compilation units are tagged (internally in the tools) with their domains.

iii. The module names in MODULE and USE statements have scope limited to their domains.

This will solve simple cases like the one above. This information would be sufficient to create a Makefile.

However it will get more complicated than that. There will be overlapping domains, so we will need an hierarchy of domains.

Of course, for programs where this situation does not arise we can simply have a single default domain and there is no need for the domain construct. That is where we are now with fpt - we didn’t see this coming. We will, of course, create a command to rename the offending modules so that the code becomes standard conforming.

The coding of all this will take a day or two, so if anyone has any better ideas please tell me!

John