Fortran and C++

T

To Forum

hi,
how can i use existing library in Fortran for my C++ program.
I have try f2c but it is rather cubersome... Can I compile the Fortran code
into Object file and then link to C++ code?
My thanks
TF
 
E

E. Robert Tisdale

To said:
How can I use existing library in Fortran for my C++ program.
I have try f2c but it is rather cubersome.
Can I compile the Fortran code into Object file and then link to C++ code?

Yes.
But the details depend upon your target platform.

platform = machine architecture, operating system,
C++ compiler and Fortran compiler.
 
J

Jack Klein

hi,
how can i use existing library in Fortran for my C++ program.
I have try f2c but it is rather cubersome... Can I compile the Fortran code
into Object file and then link to C++ code?
My thanks
TF

The C++ language does not define a binding to any other language
except for C. You need to ask in a group that supports your platform,
or your specific FORTRAN and C++ compilers to see if they provide this
ability.
 
J

Jacek Dziedzic

To said:
Can I compile the Fortran code
into Object file and then link to C++ code?
My thanks

In practice, usually yes. You'll have to

a) wrap the declaration inside 'extern "C"', as if you were
using C code
b) remember how Fortran passes parameters (like ints are
passed by pointer, not by value)

Also, you'll *probably* have to append an underscore ('_') to
C++ occurrences of the Fortran name and write them in
lower-case.

I also vaguely recall that Fortran indexes the arrays the
other way round (apart from indexing from 1), you might take
that into account.

An example:

// This uses the function DSYEVD from LAPACK, compiled with
// Fortran. In Fortran-land this function takes parameters
// of type: CHARACTER, CHARACTER, INTEGER, DOUBLE PRECISION,
// INTEGER, DOUBLE PRECISION, DOUBLE PRECISION,
// INTEGER, INTEGER, INTEGER, INTEGER.
// Notice how they are all passed by pointer, not by value

extern "C" void dsyevd_(char *job, char *uplo, int *n, double *a,
int *lda,double *w, double *work,
int *lwork,int *iwork, int *liwork, int *info);

HTH, use google,
- J.
 
E

E. Robert Tisdale

Jack said:
The C++ language does not define a binding to any other language
except for C.

The C++ programming language does *not* define a *binding*
to any other computer programming language. Not even C.
It provides a mechanism -- extern "C" --
to *help* with *linkage* to external C functions.
It does *not* guarantee that a call to function
compiled by a C compiler or even another C++ compiler
will be successful.
 
A

Arijit

To Forum said:
hi,
how can i use existing library in Fortran for my C++ program.
I have try f2c but it is rather cubersome... Can I compile the Fortran code
into Object file and then link to C++ code?
My thanks
TF

I think its possible - depending on your compiler though. If your
C++ and fortran compilers are from the same vendor, then it is
almost certainly possible. If they are from different vendors,
it might be possible - for example a few vendors provide fortran
compilers whose object files can be linked to VC++. You should try
asking in the newsgroup for your appropriate compiler.
One other thing - Fortran and C++ uses different calling conventions,
so that has to be taken care of.

-Arijit
 
W

William Clodius

To Forum said:
hi,
how can i use existing library in Fortran for my C++ program.
I have try f2c but it is rather cubersome... Can I compile the Fortran code
into Object file and then link to C++ code?
My thanks
TF
Ultimately check your compiler manuals. One thing to watch out for is
that Fortran 2003 (now in its final stages of standardization) defines
means to interoperate with C. Several F95 compilers already implement
this as an extension. If your compiler implements this, then, combined
with the appropriate use of extern "C", you may be able to do what you
want with a fair degree of long term portability.
 
E

E. Robert Tisdale

William said:
Ultimately check your compiler manuals.
One thing to watch out for is that Fortran 2003
(now in its final stages of standardization) defines means
to interoperate with C. Several F95 compilers already implement
this as an extension. If your compiler implements this, then, combined
with the appropriate use of extern "C", you may be able to do what you
want with a fair degree of long term portability.

Unfortunately, Fortran 03 interoperability is one sided.
It helps Fortran 03 programmers call C library functions
but it really won't help C or C++ programmers call Fortran subprograms.
It certainly will *not* help C and C++ programmers
call legacy Fortran 77/90/95 subprograms.
 

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

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,474
Latest member
AntoniaDea

Latest Threads

Top