MATLAB与外部C ++函数:coder.ceval传递一个结构的函数 [英] Matlab with external C++ function: coder.ceval pass a structure to a function
问题描述
我想从Simulink中Matlab的功能结构传给使用coder.ceval()和coder.cstructname外部C ++函数()。当我尝试在Simulink中使用的部署,以五金工具的一个Arduino板由于运行code我得到的错误:
I’m trying to pass a Structure from a Matlab function in Simulink to an external C++ function using the coder.ceval() and the coder.cstructname(). When I try to run the code on an Arduino Due board using the deploy to hardware tool in Simulink I get the error:
error: invalid use of incomplete type 'struct MyStruct'
error: forward declaration of 'struct MyStruct'
我使用The MathWorks公司从这个例子code,但使用C ++函数,而不是C函数:
I’m using the example code from mathworks but use a c++ function instead of a c function:
头use_struct.h:
Header use_struct.h:
#include <tmwtypes.h>
typedef struct MyStruct
{
double s1;
double s2;
} MyStruct;
void use_struct(struct MyStruct *my_struct);
C ++函数use_struct.cpp:
C++ function use_struct.cpp:
#include <stdio.h>
#include <stdlib.h>
// #include "use_struct.h" // Doesn’t work when I include it here
extern "C" void use_struct(struct MyStruct *my_struct)
{
double x = my_struct->s1;
double y = my_struct->s2;
}
MATLAB函数:
Matlab function:
structVar.s1 = 1;
structVar.s2 = 2;
if strcmp(coder.target,'rtw'),
coder.cinclude('use_struct.h');
coder.cstructname(structVar, 'MyStruct', 'extern');
coder.ceval('use_struct', coder.ref(structVar));
end
我需要它为后来的code C ++函数。不过,我也没有为externC的C函数尝试过,但它不反正工作。谁能帮我解决这个问题?
I need it to be a C++ function for the later code. However I also tried it with a c function without the extern "C" but it doesn’t work anyway. Can anyone help me with this problem?
推荐答案
我找到了解决办法。我必须包括在use_struct.cpp也与C头use_struct.h:
I found the solution. I had to include the c header use_struct.h in the use_struct.cpp also with:
extern "C"
{
#include "use_struct.h"
}
这篇关于MATLAB与外部C ++函数:coder.ceval传递一个结构的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!