首页 > 解决方案 > 使用外部 C++ 代码从 MATLAB 代码生成 C 代码

问题描述

我正在尝试使用一些外部 C++ 代码从 MATLAB 代码生成 C 代码。为了调试我的问题,我做了一个简单的例子。我将 foo.cpp 创建为:

#include "foo.h"
using namespace std;

double foo(double in1, double in2)
{
    return in1 + in2;
}

带有名为 foo.h 的头文件:

double foo(double in1, double in2);

然后,我写了一个MATALB代码:

  function r = callfoo(x,y)
      r=1.1;
      if coder.target('MATLAB')
         % Executing in MATLAB, call MATLAB equivalent of
         % C function foo
          r = x + y;
      else
         % Executing in generated code, call C function foo
         coder.updateBuildInfo('addSourceFiles','foo.cpp');
         coder.cinclude('foo.h');
         r = coder.ceval('foo', x, y);
     end
  end

但它无法生成 C 代码,因为我从 coder 收到这条消息时失败了:

构建错误:C 编译器产生错误。有关详细信息,请参阅构建日志。

当我指出更多细节时,它指向我:https ://www.mathworks.com/help///coder/ug/compiler-and-linker-errors.html

这个例子我取自 Mathworks,实际上(https://www.mathworks.com/help/simulink/slref/coder.ceval.html)。但是,他们的外部代码是 C,我的是 C++。

如何从生成的 C 代码中调用外部 C++ 代码?

标签: c++cmatlab

解决方案


推荐阅读