Thread View: comp.lang.fortran
6 messages
6 total messages
Started by Niels Hilbrink
Tue, 30 Jan 1996 08:31
How to link Fortran with c++ ??
Author: Niels Hilbrink
Date: Tue, 30 Jan 1996 08:31
Date: Tue, 30 Jan 1996 08:31
25 lines
1213 bytes
1213 bytes
Hi, I have to link a piece of fortran code with c++ code but since if never done that before I'd like to know a) can it be done and b) is it difficult. Everything concerning this subject is welcome. Gr. Niels Hiblrink -- The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. ------------------------------------------------------------ Koen D'Hondt Niels Hilbrink koen@@dutlhs1.lr.tudelft.nl niels@dutlcc3.lr.tudelft.nl **** Ripley Software Development **** (finger niels@dutlcc3.lr.tudelft.nl for more information.) ------------------------------------------------------------
Re: How to link Fortran with c++ ??
Author: Carsten Arnholm
Date: Wed, 31 Jan 1996 14:42
Date: Wed, 31 Jan 1996 14:42
186 lines
6402 bytes
6402 bytes
Niels Hilbrink <Niels@imacsg1.epfl.ch> wrote:
>Hi,
>
>I have to link a piece of fortran code with c++ code but since if never
>done that before I'd like to know a) can it be done and b) is it
>difficult.
>
>Everything concerning this subject is welcome.
>
>Gr.
>Niels Hiblrink
>
>
>--
>
> The most overlooked advantage to owning a computer is
> that if they foul up there's no law against wacking
> them around a little.
>
>------------------------------------------------------------
>Koen D'Hondt Niels Hilbrink
>koen@@dutlhs1.lr.tudelft.nl niels@dutlcc3.lr.tudelft.nl
>
>**** Ripley Software Development ****
>(finger niels@dutlcc3.lr.tudelft.nl for more information.)
>------------------------------------------------------------
Hi there,
Mixed language programming (C++/FORTRAN 77) is not as hard as
it may seem.
The only non-trivial problem you must solve is fortran CHARACTERs.
You need to write a minimal CHARACTER class in C++.
If you require single-source portability, it becomes slightly
more complicated, but not impossible.
Below follows a summary of some stuff that I have done, plus
some additional thoughts.
typedef int INTEGER;
typedef float REAL;
typedef int LOGICAL;
#include "character.h" // CHARACTER class (see below)
#define SUBROUTINE extern "C" void __stdcall
#define INTEGER_FUNCTION extern "C" INTEGER __stdcall
#define REAL_FUNCTION extern "C" REAL __stdcall
The CHARACTER class may look something like this:
#include <string.h>
/*
class CHARACTER
===============
A minimal class used when passing C-string arguments from C++ to
FORTRAN 77 (received as FORTRAN 77 CHARACTER strings), and subsequently
returned back to C++ as properly zero terminated c-strings.
Method used for zero-termination:
=================================
When the CHARACTER destructor is activated (typically when the
CHARACTER object goes out of scope within a C++ stub function)
the zero-termination of the c-string is automatically managed.
FORTRAN Assumptions:
====================
(1) FORTRAN truncates assigned strings when CHARACTER variable is too short
(2) FORTRAN pads CHARACTER variable with blanks when assigned string is short
(3) FORTRAN represents a CHARACTER as a string pointer followed by a length
Author: Carsten A. Arnholm, 20-AUG-1995
*/
class CHARACTER {
public:
CHARACTER(char* cstring) : rep(cstring), len(strlen(cstring)) {};
CHARACTER(char* cstring, const size_t lstr);
~CHARACTER();
public:
char* rep; // Actual string
size_t len; // String length, used by FORTRAN intrinsic function LEN()
};
inline CHARACTER::CHARACTER(char* cstring, const size_t lstr) : rep(cstring), len(lstr-1)
{
size_t actual = strlen(rep);
for(size_t i=actual;i<len;i++) rep[i]=' ';
rep[len]='\0';
}
inline CHARACTER::~CHARACTER() {
for(size_t i=strlen(rep)-1;i>=0;i--) {
if(rep[i] != ' ') {
rep[i+1] = '\0';
break;
}
}
}
now you can declare FORTRAN subroutine prototypes in C++:
SUBROUTINE MYSUB(INTEGER& I1, CHARACTER C1, INTEGER& I2);
To create a portable solution, do the following:
a) Write header files for your FORTRAN libraries as shown above.
b) Compile FORTRAN code and create *.lib files.
c) Include the FORTRAN library header files in your C++ code.
d) Call FORTRAN according to above prototype example.
e) Compile C++ code and link with FORTRAN libraries
f) This executes directly for Visual C++ 2.1 & 4.0 in combination
With Fortran Powerstation
porting to other machines:
a) Leave C++ and Fortran code untouched (single source requirement!)
b) Write a small C++ program that read your FORTRAN library header files,
and generate:
b.1: a platform-specific header file for the fortran
library. Here you deal with upper/lowercase names,
trailing underscores, character lengths etc.
b.2: a platform-specific *.CPP file containing inline
functions that convert from
MYSUB(INTEGER& I1, CHARACTER C1, INTEGER& I2)
to
mysub_(int& I1, char* C1.rep, int& I2, int C1.len)
(example above is for for SGI)
c) Compile & link this generated code with your application.
e) Should execute on basically any platform (UNIX, VMS, ...)
Other issues
============
a) Calling/linkage convention (C++).
This is taken care of through the use of the statement: extern "C"
This avoids name mangling which is not used in FORTRAN.
b) Routine names
An ANSI standard FORTRAN routine name should be defined in UPPERCASE
and max 6 characters. This does not, however, mean that the name
used when calling from C/C++ should be in uppercase. On the SGI machine
(i.e. unix) the routine must be in lowercase with a trailing underscore.
On other unix boxes, you may need a leading underscore and/or uppercase
name.
Example:
FORTRAN : SUBROUTINE MYSUB
Machine-dependent C++ prototype (SGI): extern "C" void mysub_();
c) Parameter types and argument passing
FORTRAN will always require parameters to be passed by reference
FORTRAN : SUBROUTINE MYSUB(IVAL)
INTEGER IVAL
Machine-dependent C++ prototype (SGI): extern "C" void mysub_(int& IVAL);
d) Machine dependent comparison of language types:
FORTRAN C++
======= ===
INTEGER int&
REAL float&
LOGICAL int&
CHARACTER char* (+ size_t length )
DOUBLE PRECISION double&
good luck,
Carsten Arnholm
ca@sesam.dnv.no
Re: How to link Fortran with c++ ??
Author: Niels Hilbrink
Date: Thu, 01 Feb 1996 09:31
Date: Thu, 01 Feb 1996 09:31
47 lines
2184 bytes
2184 bytes
Carsten Arnholm wrote: > > Niels Hilbrink <Niels@imacsg1.epfl.ch> wrote: > >I have to link a piece of fortran code with c++ code but since if never > >done that before I'd like to know a) can it be done and b) is it > >difficult. > > > >Everything concerning this subject is welcome. > > Hi there, > > Mixed language programming (C++/FORTRAN 77) is not as hard as > it may seem. > > The only non-trivial problem you must solve is fortran CHARACTERs. > You need to write a minimal CHARACTER class in C++. > > If you require single-source portability, it becomes slightly > more complicated, but not impossible. > > Below follows a summary of some stuff that I have done, plus > some additional thoughts. As understand it, this is the way to go if you want to call fortran routines in a c++ code. But what if you want to do go the other way around ? So how do I call an data member from a c++ class in fortran ? say I want to call a member function called GetData() in fortran so how do I transplate Class->GetData() in fortran ? Thanxs, Niels -- The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. ------------------------------------------------------------ Koen D'Hondt Niels Hilbrink koen@dutlhs1.lr.tudelft.nl niels@dutlcc3.lr.tudelft.nl **** Ripley Software Development **** (finger niels@dutlcc3.lr.tudelft.nl for more information.) ------------------------------------------------------------
Re: How to link Fortran with c++ ??
Author: Burt Wilson
Date: Fri, 02 Feb 1996 08:51
Date: Fri, 02 Feb 1996 08:51
16 lines
1153 bytes
1153 bytes
Niels Hilbrink wrote: > I have to link a piece of fortran code with c++ code but since if never > done that before I'd like to know a) can it be done and b) is it > difficult. > Niels Hiblrink I was also scanning this newsgroup to find some info on linking Fortran with C++.(Using windows 3.1) I am using Borland C++ version 4.5 and Microsoft Fortran Powerstation. No luck so far. It might be possible doing this using DLL but the Microsoft Fortran makes no provision for compiling to dll, only .lib and .exe files. Can you please forward any info you might find. Burt Wilson
Re: How to link Fortran with c++ ??
Author: Carsten Arnholm
Date: Fri, 02 Feb 1996 11:36
Date: Fri, 02 Feb 1996 11:36
85 lines
2719 bytes
2719 bytes
Niels Hilbrink <Niels@imacsg1.epfl.ch> wrote:
>Carsten Arnholm wrote:
>>
>> Niels Hilbrink <Niels@imacsg1.epfl.ch> wrote:
>
>> >I have to link a piece of fortran code with c++ code but since if never
>> >done that before I'd like to know a) can it be done and b) is it
>> >difficult.
>> >
>> >Everything concerning this subject is welcome.
>>
>> Hi there,
>>
>> Mixed language programming (C++/FORTRAN 77) is not as hard as
>> it may seem.
>>
>> The only non-trivial problem you must solve is fortran CHARACTERs.
>> You need to write a minimal CHARACTER class in C++.
>>
>> If you require single-source portability, it becomes slightly
>> more complicated, but not impossible.
>>
>> Below follows a summary of some stuff that I have done, plus
>> some additional thoughts.
>
>As understand it, this is the way to go if you want to call fortran routines
>in a c++ code. But what if you want to do go the other way around ?
>
>So how do I call an data member from a c++ class in fortran ?
>say I want to call a member function called GetData() in fortran so
>how do I transplate Class->GetData() in fortran ?
>
>Thanxs,
>Niels
Well, I have never done that, but generally I guess you would have to
a) Write a C-API (i.e. a set of C++ functions with C linkage) for the
things you want to call from FORTRAN. Do this by declaring the API
functions as extern "C" (avoids name mangling).
b) Provide some kind of pointer in the API to identify objects. These
pointers are not used by FORTRAN, they're just object identifiers.
Here's my suggestion (I don't know if it works).
C++:
class myClass {
public:
myMemFunc();
}
C-API:
extern "C" int myClass_create()
{
myClass* myObject = new myClass;
return (int) (*myObject);
}
extern "C" void myClass_myMemFunc(int& object)
{
myClass* myObject = (myClass*) &object;
myObject->myMemFunc();
}
FORTRAN:
PROGRAM TEST
INTEGER MYOBJ
MYOBJ = myClass_create();
myClass_myMemFunc(MYOBJ)
END
Anyone done this, or similar ?
Carsten
ca@sesam.dnv.no
Re: How to link Fortran with c++ ??
Author: M. Ghassempoory
Date: Fri, 02 Feb 1996 12:42
Date: Fri, 02 Feb 1996 12:42
23 lines
1737 bytes
1737 bytes
In article <3110887E.41C6@imacsg1.epfl.ch>, Niels@imacsg1.epfl.ch says...
>
>As understand it, this is the way to go if you want to call fortran routines
>in a c++ code. But what if you want to do go the other way around ?
>
>So how do I call an data member from a c++ class in fortran ?
>say I want to call a member function called GetData() in fortran so
>how do I transplate Class->GetData() in fortran ?
>
This is rather more difficult. To my knowlegde there are no standard
methods for C++ call handling. So if your get this working by some trick
you may fail to make it work on any other machine. You could write a small C++
jacket which accepts opject A by reference and calls A.GetData() and call this
jacket from fortran:
CALL GET_DATA_JACKET(OBJ, DATA)
However the C++ compiler mangles the names of routines. So your GET_DAT_JACKET
symbol would not be resolved unless you have a look at the map files for the
true name (Something like GET_DATA_JACKETaB_12_876YFgDR... ) and use that name
in fortran. Some compilers like GCC will let you define the true name by
__asm__ directive. It is all very tricky and messy and not worth it at all.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads