Translation Fortran >> C++

Dear C++ enthusiastics,

I want to use within a C++ program a subroutine written in Fortran.

The Fortran declaration is:
------
subroutine initialize_HiggsSignals(nHiggsneut,nHiggsplus,Expt_string)
integer,intent(in) :: nHiggsneut
integer,intent(in) :: nHiggsplus
character(LEN=*), intent(in) :: Expt_string
------

I tried to call this routine using:
------
extern "C" { void initialize_higgssignals_(int* nH, int* nHplus, string* Expt_string) ;}
------

but the 3rd argument cannot be a string. The problem is there is no char with variable length in C++.

I have tried various ways like using the 'CHARACTER' class, but the compilation keeps on crashing..

Any idea is welcome,

thanks in advance,

Xavier
I don't know anything about Fortran but char* (or const char*) is often used for strings in C.
Last edited on
Fortran functions take buffers as char*, and buffer lengths as additional (hidden from Fotran programs, but exposed for C linkage) arguments at the end of the argument list. Depending on the OS and compiler, the arguments may be integers passed by value, as pointers, or some other ways (as structs with extra data).
I've only seen them passed by value with type long, in practice (on ibm/aix, sun/solaris, and gnu/linux compiler/platform combinations)
Last edited on
There is no standard Fortran calling convention. Not only the data types, but their order and size are up to the compiler.

Your Fortran compiler must document the binary interface to be used from C, and your, er, C compiler must support that interface. When I did this in the past, the C compiler required the function to be declared with a special keyword to tell the compiler to use the Fortran convention.

If the Fortran code doesn't change frequently, you might want to have a look at f2c.
Thanks for your answers,
I have tried the most straighforward translation:

C++ code:
1
2
3
char results[20];
strcpy(results, "latestresults");
initialize_higgssignals_( &nH, &nHplus, results );


It compiles, but the Fortran function does not recognize the 3rd argument (it is the name of a directory but when printing the name out it is empty).
The straightforward translation is
initialize_higgssignals_(&nH, &nHplus, results, 20);

(don't forget to declare it in C++ accordingly, your declaration is missing the length parameter)

Oh, and you will most likely have to space-pad your string
Last edited on
If you have the fortran source code you could try f2c on it

http://en.wikipedia.org/wiki/F2c
Topic archived. No new replies allowed.