Customize argument call for embedded C++ coder interface

Hi,
I have a c library that has a function I'm using in a simulink model as included custom code of the form.
void funName(double arg1, double arg2, double arg3, double arg4[3][3])
I'm calling this function in a simulink custom funciton block with the
out = call_funName(in1,in2)
arg1 = double(0.0);
arg2 = double(0.0);
arg3 = double(0.0);
arg4 = double([0,0,0;0,0,0;0,0,0]);
arg1 = in1;
arg2 = in2;
%insert more algroithm here but just trying to get the basic zero values to generate and build for now
coder.ceval('-layout:rowMajor','funName',arg1, arg2, arg3,coder.wref(arg4));
out = arg4;
I'm trying to autocode this basic model with just this function using the basic C++ embedded coder. I'm generating the code for C++ with dimensions perserved (after setting the code mappings interface storage class to localized), I get an entry point function call that is generated as the following
funName(arg1_value,arg2_value,arg3_value, &arg4_value[0][0]);
where the declaration is correctly perserving dimensions
real_T arg4_value[3][3];
which is the correct declaration for c++ but the argument casting '[0][0]' is not.
I can generate this same model in C code and the code builds just fine because it can use a simpler declaration of
real_T arg4_value[9];
and then the argument address-of operator just tells the funciton to pass by reference whatever starts in the array address to the function, the c function then changes what ever is in that address block for the array and then the values at that address can be picked up after the function is call is finished.
However c++ does not like the argument with [0][0]. I think it needs the variables declaration to have the full [3][3] dimensions but the argument with the address-of to only include the first index dimension since the target c code argument is expecting a double array[3][3]. For example
funName(arg1_value,arg2_value,arg3_value, &arg4_value[0]);
or
funName(arg1_value,arg2_value,arg3_value, arg4_value);
builds with no complaints in C++ with the full dimensional declaration. My question is since I have a whole library of C code I need to integrate with a simulink model and then autocode the model I would really like to do this in c++ so as to take advantage of the standard libraries for my end use.
Is there a way to customize the code generation for these function calls to the c-code in the c++ code generation? Any suggestions on my implementation, I'm a little lost with how to implement this correctly to get the results I'm looking for.
Thanks

 采纳的回答

Answer from Mathworks below,
"
After collaborating with some of my colleagues it seems like this may be a limitation of C++ code generation where multidimensional arguments are not supported.
I have been testing different workarounds and it seems like there may be 2 possibilities here.
One possible workaround is to change the array call double r[3][3] into double* r. This would require changes in your code to work with this input.
The second possible workaround is to create a wrapper function that that gets called in place of the function and takes in the double* r argument and then gets casted into the double [3][3] type. The wrapper would look as follows:
void funName_wrapper(double arg1, double arg2, double arg3, double* arg4_value)
{
funName(arg1, arg2, arg3, (double(*)[3])arg4_value);
}
"
I've implemented a header and wrapper function as follows which after setting the custom code target to c++ works,
void funName_wrapper(double arg1, double arg2, double arg3, double arg4_value[9])
{
funName(arg1, arg2, arg3, *reinterpret_cast<double(*)[3][3]>(&arg4_value[0]));
}

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Deployment, Integration, and Supported Hardware 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by