c++ - 在 C 代码中为 Matlab/LabView 接口(interface)创建 DLL 包装器

标签 c++ c matlab dll labview

我想用 Matlab Compiler SDK 构建一个可以从 LabView 调用的 DLL。如您所知,LabView 不能直接调用由 Matlab Compiler SDK 生成的库(我们称之为“my_dll.dll”/“my_dll.lib”),因为它使用非标准数据类型 (mxArrays)。

因此,我的计划是在 C 中生成一个包装代码。这个包装代码,无论何时被调用,都会执行 Matlab 运行时引擎所需的例程,并调用在“my_dll.dll”中生成的函数。此包装代码基本上与使用先前生成的 DLL 的任何其他可执行文件或应用程序非常相似,遵循 this 的准则。 .

代码示例:

int main(double *input){

    /* Call the mclInitializeApplication routine. Make sure that the application
 * was initialized properly by checking the return status. This initialization
 * has to be done before calling any MATLAB API's or MATLAB Compiler SDK generated
 * shared library functions. */

   mclmcrInitialize();

   if( !mclInitializeApplication(NULL,0) )
   {
       fprintf(stderr, "Could not initialize the application.\n");
       return -1;
   }

return mclRunMain((mclMainFcnType)wrapper_main,0,NULL);

}


double wrapper_main(int argc, char **argv) {

//declare variables
double *out; // Here my output will be stored
double number = 16; // This is the input number

// Initialise library
dll_layer1Initialize();

//Create two pointers of mxArray type to store inputs and outputs
mxArray *in1_ptr;
mxArray *out1_ptr = NULL;

//Allocate input pointer to a 1 by 1 double, real matrix
in1_ptr = mxCreateDoubleMatrix(1, 1, mxREAL);

//Move the data from the input to the pointer
memcpy(mxGetPr(in1_ptr), &number, 1 * sizeof(double));

//Pass values to mlfFoo and receive in mxArray type variable
//mlfFoo is my matlab function. In this case it only performs
//the square root of the input number.
mlfFoo(1, &out1_ptr, in1_ptr);

out = mxGetPr(out1_ptr);

printf("\n Result is: %f", *out);

//Terminate foo implementation
dll_layer1Terminate();
mclTerminateApplication();

return 0;
}

但是,为了从 LabView 调用这个 C 包装代码,我必须生成一个新的 DLL,问题来了,因为我找不到这样做的方法。下图总结了我想做的事情。

enter image description here

我尝试过的事情:

1 - 使用 Matlab Compiler SDK 'mbuild' 命令编译 C 包装程序代码和 my_dll.lib 以生成 wrapper.dll。这只会创建一个可执行文件,而不是一个 dll。也许我遗漏了什么。

2 - 使用 Visual Studio 创建 DLL。我在这里面临的问题是,当我要构建项目时,它会给我很多错误,例如:

Error LNK2019 unresolved external symbol _mclRunMain_proxy referenced in function "public: static double __cdecl Wrapper::Functions::wrapper_sqrt(double)" (?wrapper_sqrt@Functions@Wrapper@@SANN@Z) Win32Project1 ...\visual studio 2015\Projects\Win32Project1\Win32Project1\wrapper.obj 1

也许我的错误是我没有正确链接 Matlab 库,在这种情况下我不知道如何修复它。

在 LabView 中运行 Matlab 代码的其他可能性是使用 MathScript 节点、Matlab Script 节点或 Matlab Coder(也用于创建 DLL),但是,出于其他几个原因,我放弃了这些选项。

预先感谢您的帮助。

最佳答案

对于尝试 #2,您是否在项目中添加了对 matlab dll 的引用?

关于c++ - 在 C 代码中为 Matlab/LabView 接口(interface)创建 DLL 包装器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46928129/

相关文章:

c - 使用 Flex+Bison 识别尾递归函数并将代码转换为迭代形式

c - 终端背景颜色并不总是使用 "\033[0m"正确重置

matlab - 在 MATLAB 中输出 4 路音频

matlab - Matlab loglog 图中的轴相等

matlab - 在Matlab中使用离散参数进行优化

c++ - 在 C++ 中不使用显式 for 循环将 int [] 转换为字符串

c++ - 将对象写入文件时应用程序崩溃

c++ - cocos2d-x 从子运行父操作

c++ - 服务器和客户端套接字连接问题重新。 send()、accept() 和多线程

c - 仅在 Visual Studio 中报告 C 警告