Calling a fortran subroutine from Visual C++

G

Guest

I have a C++ program written and compiled on Solaris 8 with gcc. With gcc
lets me compile fortran code along with the C++ program to be able to call
the fortran code as a subroutine. The specifications for this project have
changed and I am porting the C++ code to Visual C++. Is there a way to call
fortran code from a C++ program compiled with Visual C++?
 
W

William DePalo [MVP VC++]

Ray J. said:
I have a C++ program written and compiled on Solaris 8 with gcc. With gcc
lets me compile fortran code along with the C++ program to be able to call
the fortran code as a subroutine. The specifications for this project
have
changed and I am porting the C++ code to Visual C++. Is there a way to
call
fortran code from a C++ program compiled with Visual C++?

It's not clear to me what tools you have. Do you have a Fortran compiler for
Win32? If you do, and if it allows you to build a DLL, then the expedient
thing to do is export your function from the DLL and to call it from a C++
executable. Assuming compatible types and calling conventions VC++ should
have no trouble calling into an external DLL.

I don't believe that MS sells a Fortran compiler. You will find some free
Fortran compilers here:

http://www.thefreecountry.com/compilers/fortran.shtml

though I should tell you that I have no experience with any of them.

Regards,
Will
 
N

Norman Diamond


I never noticed that, but maybe it's not surprising since the Gnu Fortran
compiler is closely related to gcc. Depending on your needs, have you tried
Windows Services for Unix? I've used gcc (though not Gnu Fortran) in that
environment.

Old MSDN libraries used to say how! Microsoft used to make a Fortran
compiler, they sold the product and it became the DEC Fortran compiler for
Windows, which became the Compaq Fortran compiler for Windows, and then
Compaq or HP sold the product and now it's the Intel Fortran compiler for
Windows. But MSDN libraries no longer say how to interoperate with it, so
you'll need Intel's documents.
It's not clear to me what tools you have. [...] if it allows you to build
a DLL, then the expedient thing to do is export your function from the DLL
[...]

The big questions are what the types are, and whether there are arrays and
stuff like that. By the way the declarations in the C++ program will surely
need to be wrapped in extern "C" {}.
You will find some free Fortran compilers here:
http://www.thefreecountry.com/compilers/fortran.shtml

Interesting page, thank you. Hmm, they say that Intel's compiler is free
for personal non-commercial use. I don't think it was when I hunted it down
a while back.

They also list f2c, which I've used before. The resulting C code isn't
human readable but it does compile, and can be called from C++ callers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top