【问题标题】:Matlab with external C++ function: coder.ceval pass a structure to a function具有外部 C++ 函数的 Matlab:coder.ceval 将结构传递给函数
【发布时间】:2015-02-04 10:45:01
【问题描述】:

我正在尝试使用 coder.ceval() 和 coder.cstructname() 将结构从 Simulink 中的 Matlab 函数传递到外部 C++ 函数。当我尝试使用 Simulink 中的部署到硬件工具在 Arduino Due 板上运行代码时,出现错误:

error: invalid use of incomplete type 'struct MyStruct' 
error: forward declaration of 'struct MyStruct'

我使用的是 mathworks 的示例代码,但使用的是 c++ 函数而不是 c 函数:

头文件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:

 #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 函数:

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

我需要它成为后面代码的 C++ 函数。但是,我也尝试使用没有 extern “C” 的 c 函数,但它无论如何都不起作用。谁能帮我解决这个问题?

【问题讨论】:

    标签: c++ matlab arduino simulink matlab-coder


    【解决方案1】:

    我找到了解决方案。我还必须在 use_struct.cpp 中包含 c 标头 use_struct.h:

    extern "C"
     {
       #include "use_struct.h"
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-06
      • 1970-01-01
      • 2019-12-13
      • 2013-04-12
      • 2015-01-03
      相关资源
      最近更新 更多