如何将Matlab类编译为C lib? [英] How to compile Matlab class into C lib?
问题描述
这个问题的起源是从这里中调用的matlab函数中的变量.
The origin of this question is from here How to use "global static" variable in matlab function called in c.
我正在尝试将全局变量"封装到一个对象中.但是我不知道如何使用 MATLAB编译器将matlab类导出到c ++( mcc)
I'm trying to encapsulate the "global variable" into an object. However I don't know how to export the matlab class to c++ using MATLAB Compiler (mcc)
为此,我只是尝试了标准命令
To do this I just tried the standard command
mcc -W cpplib:Vowel4 -T link:lib Vowel4.m
Matlab脚本
classdef Vowel4
properties
x
y
end
methods
Vowel4
A
B
end
end
生成的lib实际上是独立函数,而不是c ++类.
The generated lib is actually stand-alone functions rather than c++ class.
如何将Matlab中的类编译为c ++类?
How can I compile classes in Matlab into c++ classes?
我一直在寻找答案,但是没有找到答案.
I've been searching for an answer but didn't find one.
显然,matlab命令不适用于这种情况.但是,我找不到有关将Matlab类构建到c ++类中的任何信息.
Obviously the matlab command is not suitable for this scenario. However I cannot find any information on building Matlab classes into c++ classes.
=========================编辑==================== ====
========================== Edit ========================
实际的cpp代码如下:@Alan
The actual cpp code is as follows: @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(),它们在主循环中完成工作.
Here the matlabA() corresponds to the loadData(), which loads the data, and soundByCoef(F) corresponds to the matlabB(), which do the job in the main loop.
推荐答案
正如艾伦(Alan)所说,我只是使用handle类作为全局变量的容器(这样的对象将通过引用传递).创建的对象不希望由您的C ++代码直接操作(它将存储在通用的mxArray/mwArray
C/C ++结构中).
As noted by Alan, I was only suggesting using handle class as a container for your global variables (with the benefit that such an object would be passed by reference). The created object is not intended to be directly manipulated by your C++ code (it will be stored in the generic mxArray/mwArray
C/C++ struct).
As far as I know, you cannot directly compile classdef-style MATLAB classes into proper C++ classes when building shared libraries using the MATLAB Compiler. It only supports building regular functions. You could create functional interfaces to MATLAB class member methods, but that's a different story...
也许一个完整的例子将有助于说明我的想法.首先让我们在MATLAB端定义代码:
Perhaps a complete example would help illustrate the idea I had in mind. First lets define the code on the MATLAB side:
这是用于存储全局变量的句柄类.
This is the handle class used to store the global vars.
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
充当上述类的构造函数的包装器函数
create_globals.m
A wrapper function that acts as a constructor to the above class
function globals = create_globals()
%CREATE_GLOBALS Instantiate and return global state
globals = GlobalData();
globals.val = 2;
end
fcn_add.m,fcn_times.m
MATLAB函数公开为C ++函数
fcn_add.m, fcn_times.m
MATLAB functions to be exposed as C++ functions
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 ++共享库:
With the above files stored in current directory, lets build the C++ shared library using the MATLAB Compiler:
>> mkdir out
>> mcc -W cpplib:libfoo -T link:lib -N -v -d ./out create_globals.m fcn_add.m fcn_times.m
您应该期望以下生成的文件以及其他文件(我在Windows计算机上):
You should expect the following generated files among others (I'm on a Windows machine):
./out/libfoo.h
./out/libfoo.dll
./out/libfoo.lib
接下来,我们可以创建一个示例C ++程序来测试该库:
Next, we could create a sample C++ program to test the library:
// 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类编译为C lib?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!