Passing variables between subroutines in different files using Fortran

Click For Summary

Discussion Overview

The discussion revolves around the challenges of passing variables between subroutines in different files using Fortran, specifically in the context of modifying a Monte Carlo program. Participants explore technical solutions, coding practices, and compilation strategies related to this issue.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • Sheldon describes encountering a segmentation fault when attempting to pass a 2x1 array from one subroutine to another across different files, despite checking type declarations.
  • One participant suggests a compilation method using gfortran to link multiple files, indicating that this approach worked for them.
  • Another participant recommends looking into MODULES in Fortran for better organization of subroutines, noting that they are applicable from Fortran 90 onwards.
  • There is a mention of using interfaces for enhanced functionality when working with multiple files.
  • A suggestion is made to check the compilation with the -ffree-form flag.

Areas of Agreement / Disagreement

Participants present various approaches and suggestions, but there is no consensus on a single solution to Sheldon's issue. Multiple competing views on best practices for organizing and compiling Fortran code are evident.

Contextual Notes

Limitations include potential misunderstandings of Fortran's file handling and compilation requirements, as well as the specific configurations of Sheldon's existing program setup.

Who May Find This Useful

This discussion may be useful for individuals working with Fortran, particularly those dealing with subroutine organization, variable passing, and compilation issues in multi-file projects.

SheldonM
Messages
1
Reaction score
0
Good day,

I'm modifying a Monte Carlo program (MCCCS Towhee) using Fortran and am having trouble passing an argument (a 2x1 array) from a subroutine withing one file (the existing main loop that 'drives' the code) to another subroutine written in another file (an energy calculation, also part of the existing program package). I've written another subroutine (call it 'AAA') in the main loop that receives the array, assigns it to another variable name (same type, size); then, in the other file, I call AAA to receive the array. The code compiles and runs fine until the call to the subroutine is made, and then it exits with a segmentation fault error (which is strange because I've checked and rechecked the type declarations - they seem fine). I'm a newbie to Fortran, so I'm probably not seeing a very simple solution. Since this program has a sophisticated configure and Makefile setup, and if the solution to my problem means that I must create a new *.F file, what changes (if any) must be be made to the files associated with configure and make?

Many thanks,
Sheldon
 
Technology news on Phys.org
Hi Sheldon-

I hope this answers your question since it worked for me. I run my FORTRAN code on Snow Leopard. Now I have multiple subroutines in different files that I call in my main program. The way I made it work was to do:

gfortran -o mainExec mainprogram.f file1.f file2.f ...

So mainprogram.f is obviously the main program. file1.f has some number of subroutines while file2.f has some other number of different subroutines etc. Once you then run the executable (mainExec) everything should be fine. Hope that helps.
 
When including multiple files, look up MODULES for Fortran. However, I believe you can only use them in Fortran 90 and up. Either way, they make it easier to allow the programmer to package subroutines in a common file. There are also a few different things such as interfaces which allow for even greater functionality.

As smigs said, in your Makefile, each file needs to be compiled with the correct dependencies with the -c flag, and then the final executable, linking the objects with the -o flag.
 
check with -ffree-form when compiling.
 

Similar threads

  • · Replies 25 ·
Replies
25
Views
4K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 59 ·
2
Replies
59
Views
12K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K