Arggh! Fortran 95 question

Clark1961

Touchdown! Greaser!
Joined
Jun 7, 2008
Messages
17,737
Display Name

Display name:
Display name:
Ok, so I mostly stopped using Fortran when it was still FORTRAN and folks were trying to settle on the f90 specification.

I've run across some f95 code that looks useful and rather than try to port it to another language it'd prolly be best to just compile it to a Windows DLL which could be called directly from VBA in Excel.

So, the question is are there any f95 gurus out there willing to point me toward a resource or tell me how to build the Fortran DLL without paying an arm and a leg for a compiler?
 
It's been a while for me too but gnu has free fortran compilers including 95. I have it on Linux but suspect it's also available for windows.

Try www.gnu.org

Joe
 
Thanks Joe - I'd gotten that far. The code compiles using gfortran (which is the gnu compiler) ported to windows. Some nice folks have made the gfortran binaries available so I didn't have to sacrifice too many braincells on that problem.

I've been able to test the code via compiled executables and it appears to work well enough to be useful. That's what lead me to the current problem:

I'm stumped when it comes to generating the DLL.
 
It's been a while but if I remember correctly it is a PIA to interface both Fortran and VBA.

If it's not a computationally expensive and repetitive operation, the easiest thing would be to make an exe and run it from VBA and interpret the output.

If you do need to make a DLL you will probably need a C module in between to deal with the weird data structures that VBA and FORTRAN use to pass data to subroutines. It's been to long for me to be specific.

Joe
 
It's been a while but if I remember correctly it is a PIA to interface both Fortran and VBA.

If it's not a computationally expensive and repetitive operation, the easiest thing would be to make an exe and run it from VBA and interpret the output.

If you do need to make a DLL you will probably need a C module in between to deal with the weird data structures that VBA and FORTRAN use to pass data to subroutines. It's been to long for me to be specific.

If all else fails I can use flat files to pass data to/from a fortan executable. I'd rather not take that clunky (and slow) of an approach.

On the VBA calls to dll side of things, VBA & Fortran play fairly well together. VBA & C have problems with arrays since they write'em in orthogonal directions. I'll have to pass some large arrays since it's an arbitrary dimensional problem.

Perhaps you're thinking of safearrays (or whatever Micro$oft calls their stuff). The proprietary arrays are a real problem but I avoid it by using generic array structures. That approach does limit what I can do with the passed arrays but I just consider the limitation in my program design and live with it. Life is so much easier when I minimize my reliance on the specialized micro$oft interpreter & compiler "features".

It's easy enough to deal with passing arrays once the rules are known but it's a bit of a pain to remember the set up. Strings are a pain but I only do numerical stuff so that's usually not an issue.

I normally use a fast compiled basic (PowerBasic) to build my homebrew dlls but have on occasion used Pascal and C. Passing the VBA arrays to my PowerBasic dll is reasonably straightforward: the first address and the dimension(s) of the array are passed. The full array is reconstituted in the dll based on that first address so it better be correct. Of course each compiler will have it's own flavor of solution.

The last time I used a Fortran dll it was compiled by a grad school buddy so we're talking mid 90's. Anyway I look at it, I'll have to learn to jump through a few new hoops.
 
Last time I touched FORTRAN was fall 1991.
 
the last time I used FORTRAN was last year, but I never did anything that sounds like what you are trying to do.
 
I managed to generate a test input dataset and hardwired it in the code this morning. I got lucky and it compiled (after some minor debugging). At least I can use that to demonstrate that the method works to a client. Maybe now they'll go ahead with a larger study.

Of course now my head hurts from working with some ugly (but very functional) fortran. I think I'll work on the Dakota's nose wheel pant this afternoon - that should eliminate the tension.:smile:
 
Haven't touched FORTRAN since they were still debating the specs for "FORTRAN 8X" which didn't quite make that decade and became FORTRAN 91
 
Have you considered translating it to C++ or something that can be easily compiled to a COM dll? I've never done FORTRAN, but if it is like any other language, learning enough to convert it should be easy enough.

I tend to pick up things pretty quickly, so if you need a hand, I wouldn't mind giving it a try.
 
Have you considered translating it to C++ or something that can be easily compiled to a COM dll? I've never done FORTRAN, but if it is like any other language, learning enough to convert it should be easy enough.

I tend to pick up things pretty quickly, so if you need a hand, I wouldn't mind giving it a try.

You could write a FORTRAN program to translate the FORTRAN source code into C...
 
I managed to generate a test input dataset and hardwired it in the code this morning. I got lucky and it compiled (after some minor debugging). At least I can use that to demonstrate that the method works to a client. Maybe now they'll go ahead with a larger study.

Of course now my head hurts from working with some ugly (but very functional) fortran. I think I'll work on the Dakota's nose wheel pant this afternoon - that should eliminate the tension.:smile:
I did a little googling and it seems like the dll creation is a feature of Microsoft Fortran, I did not find anything for gFortran, but I didn't look that hard.

While the flat file in/out is ugly it is the most efficient in terms of development effort.

There are open source tools to convert Fortran to C, I have used them a long time ago. My only comment is if you think fortran is ugly, translator output is a pretty good obfuscator. The results are to be considered unmaintainable. Keep the fortran around in case you have to make changes.

Joe
 
With some help from comp.lang.fortran I've got VBA talking to a fortran dll compiled with gfortran. The dll switches for the compiler don't seem to be obviously documented but they do work. I'm still at the elementary stage of testing but at least I can pass a few variables. The fortran skills are starting to come back but I'm still using the VBA ' for the fortran ! until the compiler scolds me.
 
Clark,

How about a brief description of making a fortran dll with gFortran?

Not that I have any plans to do it, but I'm curious so don't worry about the details.

Joe
 
here's an example to get started:
The fortran file:
test.f95

function intadd(i,j)
intadd=i+j
end

An auxiliary file:
test.def

EXPORTS intadd=intadd_

The pseudo make file:
make.bat

gfortran -s -shared -mrtd -o test.dll test.def test.f95

On the Excel side:

In VBA, insert a module and add under general / declarations:

Declare Function intadd Lib "test.dll" (i As Long, j As Long) As Long

On the worksheet add:

=intadd(cell_ref1,cell_ref2)

Make sure that Excel knows where the dll is located either by using a full path along with the file name in quotes or by having it in the current Excel directory. The dll can also be put somewhere that Window knows to look for dlls. And finally, I suppose the dll could be registered.
 
Last edited:
That last time I Fortran'ed it was when Fortran77 was new.

Ya, that brings back memories. We thought running Fortran77 on a VAX 11/780 was da bee's knees. But we still had to walk several blocks to the Comp Center to get hardcopy output...


Trapper John
 
That last time I Fortran'ed it was when Fortran77 was new.

Last time I touched FORTRAN was fall 1991.

the last time I used FORTRAN was last year, but I never did anything that sounds like what you are trying to do.

Haven't touched FORTRAN since they were still debating the specs for "FORTRAN 8X" which didn't quite make that decade and became FORTRAN 91

Ya, that brings back memories. We thought running Fortran77 on a VAX 11/780 was da bee's knees. But we still had to walk several blocks to the Comp Center to get hardcopy output...


Trapper John

There's something newer than FORTRAN 77? Who knew? :D

I haven't touched the language since the early 1980s when I tried to compile the source code for SPICE (yes, it was written in FORTRAN) on a Tandem system. Trouble was, REAL*8 isn't FORTRAN 77, but a very common extension to the language (as in, every compiler I had ever used) and the Tandem compiler was pure FORTRAN 77 with no extensions other than a few to support fault tolerance. Our software folks weren't interested in adding REAL*8 to the compiler, and I wasn't interested in re-writing the source code for SPICE to get around the problem, so it never got up and running in the Tandem environment.

Haven't touched FORTRAN since. I'll have to look for a free compiler and see if I can get a program from my aerospace days to run on a PC. When I feel like typing 5000 lines of code... Yeah, right. I think I'll go study for my IR written instead. Far more useful way to spend my time.
 
Back
Top