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!

Simple question on gfortran's module compilation

Status
Not open for further replies.

GerritGroot

Technical User
Nov 3, 2006
291
ES
Hi,

Imagine I've got a main program in the directory:
Code:
./MyProgram/main.f90
..and a module, to be compiled, in the directory:
Code:
./MyProgram/MODULES/mod_mymod.f90
To prevent a mess full of files, I like the compiled stuff in an another directory before linking it, so I send the object files to
Code:
./MyProgram/OBJECTFILES
and compile with
Code:
C:/MyProgram>gfortran -c ./MODULES/mod_mymod.f90 -o ./OBJECTFILES/mod_mymod.mod
C:/MyProgram>gfortran -c ./main.f90 -o ./OBJECTFILES/main.o

Why does gfortran then place the *.mod file not only in the chosen objectfile directory
Code:
./MyProgram/OBJECTFILES
but ALSO places a copy in the main directory
Code:
./MyProgram

For routines and functions it doesn't do that. Am I doing something wrong?

Thanks,

Gerrit
 
Use -J or -M to specify the directory for the module files.
Code:
C:/MyProgram>gfortran -c ./main.f90 -o ./OBJECTFILES/main.o -J ./OBJECTFILES
 
Thanks, I tried, but get the folloeing results:

Code:
gfortran -c ./MODULES/mod_globals.f90 -M ./OBJECTFILES/mod_globals.mod
Does not generate anything and gives the error message:
Code:
Warning: Using -M <directory> is deprecated, use -J instead

However, trying without giving a file...
Code:
gfortran -c ./MODULES/mod_globals.f90 -M ./OBJECTFILES/
gives the same error message saying I should use -J, but does generate some files:

"globals.mod" in the subdirectory ./OBJECTFILES
"mod_globals.o" in the main directory ./

Then I tried with the -J option.
Code:
gfortran -c ./MODULES/mod_globals.f90 -J ./OBJECTFILES/mod_globals.mod
This gives the error message:

Fatal Error: Can't open module file './OBJECTFILES/mod_globals.mod/globals.mod0'
for writing at (1): No such file or directory

So it seems it interpreted my intended output filename "mod_globals.mod" as a directory, so I tried without specifying a filename again, viz:
Code:
gfortran -c ./MODULES/mod_globals.f90 -J ./OBJECTFILES/

This generates the same two files as the same option with -M, but without error message, so:

"globals.mod" in the subdirectory ./OBJECTFILES
"mod_globals.o" in the main directory ./
 
You forgot to precise where to find *.mod files when compiling the main program :

gfortran -c ./main.f90 -o ./OBJECTFILES/main.o -I ./OBJECTFILES


I suggest to provide always the two flags :
-J ./OBJECTFILES -I ./OBJECTFILES

The first one indicates where creating *.mod, the second one where looking for existing *.mod
 
Sorry I don't understand this. I don't compile the main program at all during these try outs.

This time I ran a batch file with:
Code:
gfortran -c -Wall -O3 ./MODULES/mod_globals.f90	-J ./OBJECTFILES/ -I ./OBJECTFILES -o ./OBJECTFILES/mod_globals.mod

It now doesn't create a file in the main directory indeed, but creates 2 files with different names and different sizes in the ./OBJECTFILES directory:
Code:
1,281 globals.mod
  474 mod_globals.mod
-----
1,755 bytes

However, if I rerun the batch file, only the second file "mod_globals.mod" updates
 
Your command is not correct, or more precisely, it looks like a confusion between module file and object file :

Code:
gfortran -c -Wall -O3 ./MODULES/mod_globals.f90 -J ./OBJECTFILES/ -I ./OBJECTFILES -o ./OBJECTFILES/mod_globals.o

You are right about the fact that two files are created :
- the module file which name seems to be globals.mod
- the object file which name is mod_globals.o

I also suggest to keep the same base name for both files. You have too possibilities to correct that :
- replacing the instruction "MODULE globals" by "MODULE mod_globals"
- renaming "mod_globals.f90" into "globals.f90" and suppressing definitively the prefix mod_

I would prefer the second solution because the mod_ prefix is useless for a code entirely written in F90 : indeed, the only source file which is not a module is normally the main program.

If you rerun the command, then gfortran detects that the module file is unchanged and does not update it. The reason of that behavior is to avoid a cascade of useless compilations when using a makefile which works with file dates.
 
Thanks, I didn't know that gfortran was so clever (and so self-willed) to use the name inside the *.f90 file for the compiled module as well... ...so that's where it comes from!

(I also thought a compiled module was to be called an object file as well, why not actually?)

I started recently with prefixes like sub_*.f90 mod_*.f90 and fun_*.f90 because I reuse a lot of code and like that it's easier to look them up in the archeological code mess that I created over the years. :)
 
>> (I also thought a compiled module was to be called an object file as well, why not actually?)

Compiling a module always generates two files :
- the classical object file which must be used during the link phase to build up an executable program,
- the module file (suffix .mod) which must be used only when compiling another source file using that module (USE statement)

The .mod file plays a similar role as the .h file in C programming (providing the signatures of routines as well as type and data definitions).
 
Thanks! I had no clue about that, I thought .o and .mod files were the same, but with another extension only.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top