Discussion:
[Mono-list] Need advice about Mono C# + MinGW Fortran
Robert Dodier
2018-03-04 01:04:41 UTC
Permalink
Hi,

I am interested in puzzling out how to call a Fortran function from C#
on Windows. I was inspired by a question about how to get a certain
function (complex Bessel function) in C#. One way would be link a C#
program with SLATEC, which provides many math functions and is written
in Fortran.

I am working on Linux, so I am attempting this with Wine + MinGW
compilers. I have succeeded in compiling and running a Fortran program
with that combo. I have also succeeded with compiling and running a C#
program using Mono C# compiler.

Now I am trying to link a C# and DLL created from a Fortran function.
Has anyone succeeded there? Linking C# with a DLL created from a C
function would probably point me in the right direction also.

I have been trying to use some notes about linking C# and Fortran on
Windows, and trying to adapt those instructions to Wine. My most
recent attempt yields the following:

$ wine gfortran.exe -c fun1.f
$ wine gfortran -shared -o fun1.dll fun1.o -Wl,--out-implib,libfun1_dll.a
$ mcs HelloWorld.cs -r:fun1.dll
error CS0009: Metadata file
`/home/robert/.wine/drive_c/MinGW/bin/fun1.dll' does not contain valid
metadata
Compilation failed: 1 error(s), 0 warnings

fun1.f is a trivial function. HelloWorld.cs doesn't actually contain
any reference to fun1; linking with fun1.dll should be a no-op. I
searched for the "does not contain valid metadata" error but wasn't
able to figure out what it means.

If anyone has any advice on where to go from here, I would be very
glad to hear it. Thanks for your time, I appreciate it.

best,
Robert Dodier

PS. Here is fun1.f:
function fun1(x)
fun1 = x + 1.0
return
end

PPS. Here is HelloWorld.cs:
public class HelloWorld
{
static public void Main ()
{
Console.WriteLine ("Hello Mono World");
}
}
_______________________________________________
Mono-list maillist - Mono-***@lists.dot.net
http://lists.dot.net/mailman
Robert Jordan
2018-03-04 13:17:29 UTC
Permalink
Post by Robert Dodier
$ wine gfortran.exe -c fun1.f
$ wine gfortran -shared -o fun1.dll fun1.o -Wl,--out-implib,libfun1_dll.a
$ mcs HelloWorld.cs -r:fun1.dll
error CS0009: Metadata file
`/home/robert/.wine/drive_c/MinGW/bin/fun1.dll' does not contain valid
metadata
Compilation failed: 1 error(s), 0 warnings
You cannot consume native DLLs like they were .NET assemblies.
Neither with MS.NET nor with Mono, neither under Windows, nor
under Linux.

You may want to look at what "p/invoke" is, and how to leverage it.

But even with p/invoke you won't be able to consume a native Windows
DLL with Mono under Linux, unless you're resorting to Wine & Mono for
Wine (which is just Mono for Windows, IIRC).

Robert
_______________________________________________
Mono-list maillist - Mono-***@lists.dot.net
http://lists.do
Robert Dodier
2018-03-05 06:45:03 UTC
Permalink
Post by Robert Jordan
Post by Robert Dodier
$ wine gfortran.exe -c fun1.f
$ wine gfortran -shared -o fun1.dll fun1.o -Wl,--out-implib,libfun1_dll.a
$ mcs HelloWorld.cs -r:fun1.dll
error CS0009: Metadata file
`/home/robert/.wine/drive_c/MinGW/bin/fun1.dll' does not contain valid
metadata
Compilation failed: 1 error(s), 0 warnings
You cannot consume native DLLs like they were .NET assemblies.
Neither with MS.NET nor with Mono, neither under Windows, nor
under Linux.
Well, my larger goal is to be able to call a Fortran function from C#.
It's not important to me to use native DLLs or any other specific approach.
Can you suggest an approach which can lead toward the goal?
I have been reading about p/invoke but I haven't been able to devise
a solution based on that; if you have any hints that would be great.

Thanks for your help,
Robert Dodier
_______________________________________________
Mono-list maillist - Mono-***@lists.dot.net
http://lists.dot.net/mailman/list

Loading...