正如 Alan 所指出的,我只是建议使用句柄类作为全局变量的容器(这样做的好处是可以通过引用传递这样的对象)。创建的对象不打算由您的 C++ 代码直接操作(它将存储在通用mxArray/mwArray
C/C++ 结构中)。
据我所知,在使用 MATLAB 编译器构建共享库时,您不能直接将 classdef 样式的 MATLAB 类编译为适当的 C++ 类。它只支持构建常规功能。您可以为 MATLAB 类成员方法创建功能接口,但那是另一回事......
也许一个完整的例子将有助于说明我的想法。首先让我们在 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++ 程序来测试该库:
主文件
// 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
高温高压