[CMake] Fortran-C mixed code

Arjen Markus arjen.markus at wldelft.nl
Fri Jan 19 03:40:05 EST 2007


>
> But knowing the Fortran name mangling scheme is still crucial (if
> interested,  see below why). For now I rely on the user to provide the
> case (lower or upper)  and number of appended underscores (none, one,
> or two) which determine it. The  defaults I provide (lower case with
> one underscore) is probably the most common  one, but I'd be much
> happier if I could automatically determine it at  configuration time...
> Any suggestions?
>

Hello Radu,

how about this solution:

Suppose you have a C function like:

void nextStep( float t, float dt, float *x, int n, float *xnew) {
    ...
}

The Fortran wrapper would look like:

void nextstep_( float *t, float *dt, float *x, int *n, float *xnew ) {
    nextStep( *t, *dt, x, *n, xnew ) ;
}

if the Fortran compiler uses a name-mangling scheme that translates
all names to lower-case and appends an underscore.

Another Fortran compiler might require a wrapper like:


void NEXTSTEP( float *t, float *dt, float *x, int *n, float *xnew ) {
    nextStep( *t, *dt, x, *n, xnew ) ;
}

Now the fun part:
You can simply provide _both_ of these wrappers in your Fortran-compatible
library. The Fortran compiler will generate a routine with a name that
depends on its mangling scheme, but the linker will pick out the right
version.

The only problem left is Fortran compilers that produce the same
mangled name but different methods for passing the length of character
strings.
Of course, the compatibility library will be bit larger, but it solves
your problem in what I think an elegant way.

Regards,

Arjen








More information about the CMake mailing list