Specifying inputs/outputs for C-code functions generated from MATLAB m-code using emlc as pass-by-reference

2 次查看(过去 30 天)
How do I make the generated c-code use pass-by-reference for all inputs and outputs instead of pass-by-value?
I am using emlc to generate c-code for a function that has constant, scalar, array (vector), and struct inputs and outputs. I am going to be calling this c-code from FORTRAN code, so I need to use pass-by-reference for all inputs and outputs. Depending on my FORTRAN compiler, I also may need to specify a calling convention. In the past, I have been writing wrappers for the generated code, which is tedious. How can I specify the interface that I want?
  3 个评论
Todd
Todd 2011-8-18
Using A. Bottema's example below,
function y = foobar(a,b,c) %#eml
eml.cstructname(b, 'bstruct')
y = a + b.field1 + c(3);
I had been building as his example,
emlc -c -T rtw -d foobar -report foobar.m -eg { 0, struct('field1',0), zeros(3,1)}
and obtaining a c-function like:
real_T foobar(real_T eml_a, const bstruct eml_b, const real_T eml_c[3])
{
return (eml_a + eml_b.field1) + eml_c[2];
}
What I want is
void foobar(real_T *eml_a, const bstruct *eml_b, const real_T eml_c[3])
Then I can call it from FORTRAN:
CALL foobar(a,b,c)
provided I have defined the structure and array correctly.
A. Bottema's answer works, but is sort of a workaround. I still need a wrapper, but now I can write my wrapper in MATLAB. Or change how I use the function in MATLAB. It would be nice to just declare somehow what I want the interface to be...
Or, if there was a way to change how I call the function from FORTRAN...
Todd
Todd 2011-8-18
Now the problem that I'm having is that two different functions create different versions of mldivide.c, so I can't put all of the object files in the same folder. If I try to put my wrapper functions all into one file and build that, they the function names get mangled.
Is there a way to define what the function name will be for a non-top-level function? Similar to the eml.cstructname function?

请先登录,再进行评论。

采纳的回答

Alexander Bottema
Alexander Bottema 2011-8-17
To force call-by-reference for the top-level function you declare every input also as output.
Suppose you have the following function signature:
function y = foobar(a,b,c)
and you want call-by-reference on 'a', 'b' and 'c'.
Then add them as etra outputs:
function [y,a,b,c] = foobar(a,b,c)
For example:
function [y,a,b,c] = foobar(a,b,c) %#eml
y = a + b.field1 + c(3);
compiled with:
emlc -T RTW foobar.m -eg { 0, struct('field1',0), zeros(1,10) }
Will produce the C code:
real_T foobar(const real_T *a, const struct_T *b, const real_T c[10]) { return (*a + b->field1) + c[2]; }
As you can see the parameters 'a', 'b' and 'c' are now being passed by reference. In C arrays are always passed by reference so there are no need for using a pointer on that argument.
  1 个评论
Benjamin
Benjamin 2024-3-12
This is quite an old answer by now. Is it with newer Matlab releases still not possible to specifiy this explicitly with a coder or embedded coder configuration?
I'm currently dealing with similar issues and wouuld love to hear if I still need to handle this the same way as suggested here in 2011.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Coder 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by