Many Fortran code! How to call Fortran code from C/C++?

L

Luna Moon

Hi all,

As a C/C++ programmer, there are a few reasons to use Fortran:

(1) Fortran is very similar to Matlab and easy to port;
(2) Fortran has support of complex numbers and vectorized numbers and
the operations in Fortran are naturally element-wise, operating on a
whole vector.
(3) There are many scientific codes are in Fortran.

-------------

So how do I call Fortran program from my C program?

I am using MSVS. NET 2003, VisualC++ and Intel C++ therein, and also
the Intel Visual Fortran therein.

To give an example, I want to call a Fortran program from C++, which
is in its bare form, it is a function that evaluates something and
pass the results back; I also want to call C++ from Fortran, one
example is that many good numerical integral codes are in Fortran, but
I want to provide my integrand function in C++.

How to do these interfaces? Pointers and readings are appreciated!
Thanks for your help!
 
J

Jim Langston

Luna Moon said:
Hi all,

As a C/C++ programmer, there are a few reasons to use Fortran:

(1) Fortran is very similar to Matlab and easy to port;
(2) Fortran has support of complex numbers and vectorized numbers and
the operations in Fortran are naturally element-wise, operating on a
whole vector.
(3) There are many scientific codes are in Fortran.

-------------

So how do I call Fortran program from my C program?

I am using MSVS. NET 2003, VisualC++ and Intel C++ therein, and also
the Intel Visual Fortran therein.

To give an example, I want to call a Fortran program from C++, which
is in its bare form, it is a function that evaluates something and
pass the results back; I also want to call C++ from Fortran, one
example is that many good numerical integral codes are in Fortran, but
I want to provide my integrand function in C++.

How to do these interfaces? Pointers and readings are appreciated!
Thanks for your help!

From Fortan calling C++, make sure the C++ functions are declared extern "C"
so there is no mangeling going on. Then check to see how to call C
functions from Fortran.

Either way, you'll have to check your compiler's documentation. It can be
different for different compilers.
 
G

Gib Bogle

Luna said:
Hi all,

As a C/C++ programmer, there are a few reasons to use Fortran:

(1) Fortran is very similar to Matlab and easy to port;
(2) Fortran has support of complex numbers and vectorized numbers and
the operations in Fortran are naturally element-wise, operating on a
whole vector.
(3) There are many scientific codes are in Fortran.

-------------

So how do I call Fortran program from my C program?

I am using MSVS. NET 2003, VisualC++ and Intel C++ therein, and also
the Intel Visual Fortran therein.

To give an example, I want to call a Fortran program from C++, which
is in its bare form, it is a function that evaluates something and
pass the results back; I also want to call C++ from Fortran, one
example is that many good numerical integral codes are in Fortran, but
I want to provide my integrand function in C++.

How to do these interfaces? Pointers and readings are appreciated!
Thanks for your help!

RTFM
 
P

Puppet_Sock

So how do I call Fortran program from my C program?

I am using MSVS. NET 2003, VisualC++ and Intel C++ therein, and also
the Intel Visual Fortran therein.
[snap]

All the news groups you posted to, and none of them
are on-topic for your question. You need to try in a
newsgroup that has some connection to your compilers.
Try something with microsoft or windows or DOS in
the name, maybe visualC in the name, like that.
If you go to groups.google.com and look at the
news groups under microsoft.public.* you should
find some helpful areas. And IIRC, Intel maintains
some on-line forums at their web site for the Fortran
aspects.
Socks
 
R

Richard Maine

All the news groups you posted to, and none of them
are on-topic for your question.

Perhaps one should be familiar with the groups in question before making such
statements about them. I'd agree that this is obviously off-topic for
sci.math.num-analysis. I don't frequent comp.programming enough to know, but
it does seem likely to be off-topic there. I don't read comp.lang.c++, but
from what I have heard, the question might not be welcome there.

However, questions like this are definitely considered on topic in
comp.lang.fortran. I'd say it even counts as a FAQ. In fact, that's closer to
the reason I didn't take the time to answer.

--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain


________________________________________________

Hogwasher, Premier News and Mail for OS X
http://www.asar.com/cgi-bin/product.pl?58/hogwasher.html
________________________________________________
 
G

Guest

Luna Moon said:
So how do I call Fortran program from my C program?
Too bad you're not using Ada. The interface between Ada and
Fortran is easy. For that matter, ther interface between Ada and
C is easy. Maybe you could simply create an Ada wrapper so
you could make the C and Fortran Code work together in a
more straightforward manner.

Richard Riehle
 
G

Guest

LR said:
Hi Richard,



Is that true no matter your platform and compilers?
This is especially true for C. I have not yet used it for Fortran, but
the interfaces are defined at the language level with special libraries.
The mechanism is implemented through a combination of libraries
and pragmas (compiler directives). For example, a matrix in Fortran
is very different from a matrix in either Ada or C so there is a pragma
for Fortran that automatically compensates for this.

I recommend contacting AdaCore, a company in New York City
that specializes in Ada compilers. Someone there can give you
more information.

Richard Riehle
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top