Converting Fortran module<->C++ structure

A

Anonymous

I have a mixed-language pgm (Fortran and C++) which needs to pass a few
hundred values from C++ to Fortran. One way to do this is to have a
Fortran module and a C++ structure (or class) with an identical data
layout. I have used this idea with no problem, but it necessitates
making changes very carefully to the Fortran module and the C++
structure to keep them "in sync".

I am looking for a program which translates source files between these
two formats; i e, takes a Fortran module source as input and produces a
C++ structure source file as output, or vice versa. Then I will maintain
one source file and run this hypothetical program before each build to
produce the other source file.

Anyone know of such a program?

Thanks for any tips.

(e-mail address removed) (remove caps to get e-mail)
 
D

David Frank

Anonymous said:
I have a mixed-language pgm (Fortran and C++) which needs to pass a few
hundred values from C++ to Fortran. One way to do this is to have a
Fortran module and a C++ structure (or class) with an identical data
layout. I have used this idea with no problem, but it necessitates
making changes very carefully to the Fortran module and the C++
structure to keep them "in sync".

I am looking for a program which translates source files between these
two formats; i e, takes a Fortran module source as input and produces a
C++ structure source file as output, or vice versa. Then I will maintain
one source file and run this hypothetical program before each build to
produce the other source file.

Anyone know of such a program?

Thanks for any tips.

(e-mail address removed) (remove caps to get e-mail)

If your C++ data declarations are C compatible
perhaps C2F can create a module from them.

http://home.cfl.rr.com/davegemini/C2F.ZIP

C2F.exe is a windows program
 
G

Gary L. Scott

Anonymous said:
I have a mixed-language pgm (Fortran and C++) which needs to pass a few
hundred values from C++ to Fortran. One way to do this is to have a
Fortran module and a C++ structure (or class) with an identical data
layout. I have used this idea with no problem, but it necessitates
making changes very carefully to the Fortran module and the C++
structure to keep them "in sync".

I am looking for a program which translates source files between these
two formats; i e, takes a Fortran module source as input and produces a
C++ structure source file as output, or vice versa. Then I will maintain
one source file and run this hypothetical program before each build to
produce the other source file.

I personally don't know of any that does specifically this, but should
not be too difficult to write one if your definitions are fairly simple
(and even easier if you follow a consistent structure/format).

Maybe you could use both C2F and F2C. They're both "free".
Anyone know of such a program?

Thanks for any tips.

(e-mail address removed) (remove caps to get e-mail)


--

Gary Scott
mailto:[email protected]

Fortran Library
http://www.fortranlib.com

Support the GNU Fortran G95 Project: http://g95.sourceforge.net
 
D

David Frank

Anonymous said:
C2F produces no output when given a .h file with #defines as input, so
this pgm is not useful for my task.

(e-mail address removed) (remove caps to get e-mail)

try copying your .h file to a .c file
and when solicited for a filename dont include the .c extension
(its assumed)

and/or you can send me the file if you want me to try it..
 
R

Richard Maine

Anonymous said:
I have a mixed-language pgm (Fortran and C++) which needs to pass a
few hundred values from C++ to Fortran. One way to do this is to have
a Fortran module and a C++ structure (or class) with an identical data
layout.

Presumably you mean a Fortran derived type (aka structure). Fortran
modules don't *HAVE* a data layout; they are not comparable to C/C++
structures in any particularly useful ways.

I don't have a handy translator for such things (though I'll note that
in the draft f2003 you can do a Fortran derived type that is interoperable
with a C struct in a way that is directly comparable enough that a
translator would be simple).
 
E

E. Robert Tisdale

Richard said:
Presumably, you mean a Fortran derived type (aka structure).
Fortran modules don't *HAVE* a data layout;
they are not comparable to C/C++ structures
in any particularly useful ways.

A header file is as close as you can get to a module in C or C++.
I don't have a handy translator for such things (though I'll note
that, in the draft f2003, you can do a Fortran derived type
that is interoperable with a C struct
in a way that is directly comparable enough that
a translator would be simple).

Fortran 90 derived type:

type:: type_t
sequence
integer:: i
real:: x
end type type_t

Equivalent C or C++ structure:

#include <stdint.h>
typedef int32_t f77_integer; // f90 implementation dependent
typedef float f77_single; // f90 implementation dependent
typedef void f77_subroutine; // f90 implementation dependent

typedef struct type_t {
f77_integer i;
f77_single x;
} type_t;

Different Fortran 90 compilers "mangle" the names
of functions contained in modules in different ways
to form the symbols that it leaves behind in object files
for the link editor.
You will need Fortran 90 "helper" functions
to access them reliably.
For example, if the module module_h contains

subroutine f(t)
type(type_t), intent(in):: t
! do something with t
end subroutine f

you will need

subroutine help_f(t)
use module_h
type(type_t), intent(in):: t
f(t)
end subroutine help_f

Then, you can write

#ifdef __cplusplus
extern "C" {
#endif/*__cplusplus */
f77_subroutine f(const type_t* pT) {
extern f77_subroutine help_f_(const type_t*);
help_f_(pT);
}
#ifdef __cplusplus
}
#endif/*__cplusplus */
 
I

Ira Baxter

Anonymous said:
I have a mixed-language pgm (Fortran and C++) which needs to pass a few
hundred values from C++ to Fortran. One way to do this is to have a
Fortran module and a C++ structure (or class) with an identical data
layout. I have used this idea with no problem, but it necessitates
making changes very carefully to the Fortran module and the C++
structure to keep them "in sync".

I am looking for a program which translates source files between these
two formats; i e, takes a Fortran module source as input and produces a
C++ structure source file as output, or vice versa. Then I will maintain
one source file and run this hypothetical program before each build to
produce the other source file.

Anyone know of such a program?

Our DMS Software Reengineering Toolkit is generalized compiler
technology capable of parsing/transforming/prettyprinting many
languages, including Fortran and C++. By giving it various
transformation rules, you could transform your Fortran "module"
into an arbitrary corresponding set of C++ definitions.
See http://www.semanticdesigns.com/Products/DMS/DMSToolkit.html.
 

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
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top