MATLAB Answers

A King
0

C/Fortran callback to MATLAB

Asked by A King
on 14 Sep 2019 at 6:06
Latest activity Commented on by James Tursa
on 18 Sep 2019 at 15:41
I have a large library (~100K lines of code) written in modern Fortran, out of which I need to call only one function with the following C-signature,
#include <stdint.h>
void runFoo (
// nd
int32_t *
// getFuncFromMatlab(nd, Point(nd)): procedure pointer to the MATLAB function
, double (*)(int *, double [])
// input character vector
, char []
// the length of the input chracter vector
, size_t *
);
Note that this function contains a callback to MATLAB. In other words, MATLAB calls this function, and this DLL function has to call another function in MATLAB provided by the original calling MATLAB function. I can generate a DLL from this library, and it loads in MATLAB via lodlibrary() with the above C header. However, that is no help, and I believe I am seriously missing some fundamental step to make this callback work. What is the best approach to solve this problem? MATLAB mex file compilation is no help either, as it apprently cannot even understand the syntax of modern Fortran, for example, module keyword. appreciate you help very much.
As a side note, as much as I find MATLAB a great standalone computing tool, I also find it terrible for any interoperation with any other languages. Hope this issue gets fixed. Are there any interoperation capabilities in MATLAB that would be equivalent to the simple, yet extremely powerful ctypes package of Python?

  10 Comments

A King
on 15 Sep 2019 at 1:50
Thanks James, your response could take me to the next step indeed. I will have to try find that /fixed option see if that resolve the issue.
Bruno Luong
on 15 Sep 2019 at 9:18
Why can't you compile your library as *.lib then link it whith a C/C++/FORTRAN MEX interface function?
You need to figure out suitable name mangling, calling convention to suit both sides but that all you need to do (well beside of course programming the interface function).
James Tursa
on 18 Sep 2019 at 15:41
"You need to figure out suitable name mangling ..."
If you are using C++, I would highly advise using the extern "C" { ... } feature to avoid all name mangling.

Sign in to comment.

Products


Release

R2019a

1 Answer

Answer by Bruno Luong
on 14 Sep 2019 at 7:00
Edited by Bruno Luong
on 14 Sep 2019 at 7:10

Not sure what you mean by "Callback". Callback to me is kind of event trigger function like an SW interruption.
Anyway if you have the compiler, you can compile any MATLAB function in C shared library, and call it from external C program.
I have no idea about FORTRAN, but in the pass I have linked fortran (gcc,g77) with MSVSC/Intel-C, a bit tricky but doable.

  1 Comment

A King
on 14 Sep 2019 at 15:13
Hi Bruno, Thanks, the code being in Fortran is almost irrelevant here, since modern Fortran enabled virtually full interoperation with C, as if you are dealing with C code instead of Fortran. So here is the situation: The user (me) wants to call subroutine Foo() in this Fortran library, which has the above C interface. I have this library's DLL file compiled by the Intel compiler, but apparently that is not enough. So what other methods exist to call this DLL library function from inside MATLAB?
How can I call this function via MATLAB's loadlibrary()? thanks again.

Sign in to comment.