MATLAB Answers

C/Fortran callback to MATLAB

10 views (last 30 days)
A King
A King on 14 Sep 2019
Commented: James Tursa on 18 Sep 2019
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?


Show 7 older comments
A King
A King on 15 Sep 2019
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
Bruno Luong on 15 Sep 2019
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
James Tursa on 18 Sep 2019
"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.

Answers (1)

Bruno Luong
Bruno Luong on 14 Sep 2019
Edited: Bruno Luong on 14 Sep 2019
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
A King on 14 Sep 2019
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.

Sign in to answer this question.