这个问题的来源是这里How to use "global static" variable in matlab function called in c .
我试图将“全局变量”封装到一个对象中。但是我不知道如何使用 MATLAB Compiler (mcc) 将 matlab 类导出到 C++
为此我只是尝试了标准命令
Matlab 命令
mcc -W cpplib:Vowel4 -T link:lib Vowel4.m
Matlab 脚本
classdef Vowel4
properties
x
y
end
methods
Vowel4
A
B
end
end
生成的lib实际上是独立的函数而不是c++类。
如何将 Matlab 中的类编译成 C++ 类?
我一直在寻找答案,但没有找到。
显然matlab命令不适合这种场景。但是我找不到任何关于将 Matlab 类构建为 C++ 类的信息。
==========================编辑==================== ====
实际的cpp代码如下:@Alan
mclInitializeApplication(NULL, 0);
loadDataInitialize();
soundByCoefInitialize();
loadData();
mwArray F(4, 1, mxDOUBLE_CLASS);
float test[4];
for ( ;; ){
const Frame frame = controller.frame();
const FingerList fingers = frame.fingers();
if ( !fingers.empty() ){
for ( int i = 0; i < 4; i ++ ){
double v = fingers.count() > i ? (fingers[i].tipPosition().y / 50) - 2 : 0;
F(i+1,1) = v;
test[i] = v;
cout << v << ' ';
}
cout << endl;
soundByCoef(F);
}
}
这里的 matlabA() 对应于加载数据的 loadData(),而 soundByCoef(F) 对应于 matlabB(),后者在主循环中完成工作。
最佳答案
正如艾伦所说,I was只建议使用句柄类作为全局变量的容器(这样的对象将通过引用传递)。创建的对象不打算由您的 C++ 代码直接操作(它将存储在通用 mxArray/mwArray
C/C++ 结构中)。
As far作为I know ,在使用 MATLAB 编译器构建共享库时,您不能直接将 classdef 样式的 MATLAB 类编译为正确的 C++ 类。它只支持构建常规函数。您可以为 MATLAB 类成员方法创建功能接口(interface),但那是另一回事...
也许一个完整的例子可以帮助说明我的想法。首先让我们在 MATLAB 端定义代码:
全局数据.m
这是用于存储全局变量的句柄类。
classdef GlobalData < handle
%GLOBALDATA Handle class to encapsulate all global state data.
%
% Note that we are not taking advantage of any object-oriented programming
% concept in this code. This class acts only as a container for publicly
% accessible properties for the otherwise global variables.
%
% To manipulate these globals from C++, you should create the class API
% as normal MATLAB functions to be compiled and exposed as regular C
% functions by the shared library.
% For example: create(), get(), set(), ...
%
% The reason we use a handle-class instead of regular variables/structs
% is that handle-class objects get passed by reference.
%
properties
val
end
end
create_globals.m
作为上述类的构造函数的包装函数
function globals = create_globals()
%CREATE_GLOBALS Instantiate and return global state
globals = GlobalData();
globals.val = 2;
end
fcn_add.m, fcn_times.m
要公开为 C++ 函数的 MATLAB 函数
function out = fcn_add(globals, in)
% receives array, and return "input+val" (where val is global)
out = in + globals.val;
end
function out = fcn_times(globals, in)
% receives array, and return "input*val" (where val is global)
out = in .* globals.val;
end
将上述文件存储在当前目录中,让我们使用 MATLAB 编译器构建 C++ 共享库:
>> mkdir out
>> mcc -W cpplib:libfoo -T link:lib -N -v -d ./out create_globals.m fcn_add.m fcn_times.m
除其他外,您应该期望生成以下文件(我在 Windows 机器上):
./out/libfoo.h
./out/libfoo.dll
./out/libfoo.lib
接下来,我们可以创建一个示例 C++ 程序来测试该库:
main.cpp
// Sample program that calls a C++ shared library created using
// the MATLAB Compiler.
#include <iostream>
using namespace std;
// include library header generated by MATLAB Compiler
#include "libfoo.h"
int run_main(int argc, char **argv)
{
// initialize MCR
if (!mclInitializeApplication(NULL,0)) {
cerr << "Failed to init MCR" << endl;
return -1;
}
// initialize our library
if( !libfooInitialize() ) {
cerr << "Failed to init library" << endl;
return -1;
}
try {
// create global variables
mwArray globals;
create_globals(1, globals);
// create input array
double data[] = {1,2,3,4,5,6,7,8,9};
mwArray in(3, 3, mxDOUBLE_CLASS, mxREAL);
in.SetData(data, 9);
// create output array, and call library functions
mwArray out;
fcn_add(1, out, globals, in);
cout << "Added matrix:\n" << out << endl;
fcn_times(1, out, globals, in);
cout << "Multiplied matrix:\n" << out << endl;
} catch (const mwException& e) {
cerr << e.what() << endl;
return -1;
} catch (...) {
cerr << "Unexpected error thrown" << endl;
return -1;
}
// destruct our library
libfooTerminate();
// shutdown MCR
mclTerminateApplication();
return 0;
}
int main()
{
mclmcrInitialize();
return mclRunMain((mclMainFcnType)run_main, 0, NULL);
}
让我们构建独立程序:
>> mbuild -I./out main.cpp ./out/libfoo.lib -outdir ./out
最后运行可执行文件:
>> cd out
>> !main
Added matrix:
3 6 9
4 7 10
5 8 11
Multiplied matrix:
2 8 14
4 10 16
6 12 18
HTH
关于matlab - 如何将 Matlab 类编译成 C 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15747411/