Fortran Passing variables between subroutines in different files using Fortran

AI Thread Summary
The discussion revolves around a user modifying a Monte Carlo program (MCCCS Towhee) in Fortran and encountering issues with passing a 2x1 array between subroutines in different files, leading to a segmentation fault error. The user has verified type declarations but remains uncertain about the cause of the error, especially since they are new to Fortran. A response suggests that the user should compile the program by including all relevant files in the command line using gfortran, ensuring that the main program and subroutine files are correctly linked. It also recommends exploring Fortran modules for better organization of subroutines and mentions the importance of properly setting up the Makefile to handle file dependencies and compilation flags. The advice emphasizes using the -c flag for compiling individual files and the -o flag for creating the final executable, along with the -ffree-form option for compatibility.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
25
Views
3K
Replies
59
Views
11K
Replies
8
Views
4K
Replies
2
Views
2K
Replies
4
Views
2K
Back
Top