我在 MATLAB 中编写可以执行简单线性运算(例如求矩阵的逆)的 MEX 文件时遇到问题。我已经成功地使用 Visual Studio 2010 求矩阵的逆,并成功创建了 MEX 文件,因此我遇到的唯一问题是将这两个概念放在一起。我尝试编译从 MathWorks 网站获得的 MEX 示例代码,但没有成功。
这是我尝试过的,
-
保存我从 MathWorks 获得的文件(重命名为 .c 扩展名),然后尝试在 MATLAB 中编译它得到:
<块引用>
正在创建库 C:\Users\CIT\AppData\Local\Temp\mex_bKHjrl\templib.x
和对象 C:\Users\CIT\AppData\Local\Temp\mex_bKHjrl\templib.exp
eko1.obj:错误 LNK2019:引用了无法解析的外部符号 dgesv
在函数 mexFunction eko1.mexw64 中:致命错误 LNK1120:1
未解决的外部问题
保存
我还尝试将其编译为 .cpp 文件,但是发生了错误,因为它无法识别 memcpy 函数。
-
由于这些不起作用,我编写了自己的程序,该程序使用了 LAPACK 库中的子例程 dgetrf 和 dgetri,但是发生了错误:
<块引用>
c:\users\cit\documents\matlab\f2c.h(16):错误 C2371:“复杂”:
重新定义;不同的基本类型
C:\Program Files\MATLAB\R2011b\extern\include\lapack.h(39) :参见“complex”的声明 c:\users\cit\documents\matlab\f2c.h(17)
:错误 C2371:'doublecomplex':重新定义;不同的基本类型
C:\Program Files\MATLAB\R2011b\extern\include\lapack.h(40) :参见 'doublecomplex' eko2.cpp(29) 的声明:错误 C2057:
预期的常量表达式 eko2.cpp(29) :错误 C2466:不能
分配常量大小为 0 的数组 eko2.cpp(29) :错误 C2133:
'ipiv':未知大小 eko2.cpp(33):错误 C2664:'dgetrf':不能
将参数 1 从 'integer *' 转换为 'ptrdiff_t *'
指向的类型是不相关的;转换需要reinterpret_cast、C 风格转换或函数风格转换 eko2.cpp(34) :
错误 C2664: 'dgetri': 无法将参数 1 从 'integer *' 转换为
'ptrdiff_t *'
指向的类型是不相关的;转换需要reinterpret_cast、C 风格转换或函数风格转换
:
非常感谢你们给我的任何帮助
提前致谢。
I am having trouble writing a MEX file in MATLAB that can perform a simple linear operation such as taking the inverse of a matrix. I have successfully managed to take the inverse of a matrix using Visual Studio 2010 and have successfully created a MEX file hence the only thing I am having trouble is getting these two concepts together. I have tried to compile a MEX example code that I got from the MathWorks site but with no luck.
Here is what I have tried,
-
Saved the file (renamed it) I got from MathWorks as .c extension and then tried to compile it in MATLAB got:
Creating library C:\Users\CIT\AppData\Local\Temp\mex_bKHjrl\templib.x
and object C:\Users\CIT\AppData\Local\Temp\mex_bKHjrl\templib.exp
eko1.obj : error LNK2019: unresolved external symbol dgesv referenced
in function mexFunction eko1.mexw64 : fatal error LNK1120: 1
unresolved externals
-
Also I tried to compile it as a .cpp file however an error occurred because it didnt recognize the memcpy function.
-
Since these didn't work I wrote my own program that used the subroutines dgetrf and dgetri from the LAPACK library however an error occured:
c:\users\cit\documents\matlab\f2c.h(16) : error C2371: 'complex' :
redefinition; different basic types
C:\Program Files\MATLAB\R2011b\extern\include\lapack.h(39) : see declaration of 'complex' c:\users\cit\documents\matlab\f2c.h(17)
: error C2371: 'doublecomplex' : redefinition; different basic types
C:\Program Files\MATLAB\R2011b\extern\include\lapack.h(40) : see declaration of 'doublecomplex' eko2.cpp(29) : error C2057:
expected constant expression eko2.cpp(29) : error C2466: cannot
allocate an array of constant size 0 eko2.cpp(29) : error C2133:
'ipiv' : unknown size eko2.cpp(33) : error C2664: 'dgetrf' : cannot
convert parameter 1 from 'integer *' to 'ptrdiff_t *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast eko2.cpp(34) :
error C2664: 'dgetri' : cannot convert parameter 1 from 'integer *' to
'ptrdiff_t *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Any help you guys give me would be extremely appreciated
Thanks in advance.
发布评论
评论(1)
没有任何代码来查看您尝试过的内容,很难说,但是......为了在 matlab 中正确编译依赖于其他库的 mex 文件,您需要在编译命令中指定这些库。使用
mex filename.c -v -l*libraryname*.lib
。 -l 开关向编译器指示您正在指定要包含的库。如果找不到该库,我将在命令中包含该库的完整路径。我希望能为您提供一些帮助。使用这种方法对我来说是成功的。Without any code to see what you've tried, it's hard to tell, but... in order to properly compile a mex file in matlab that depends on other libraries, you need to specify those libraries in the compile command. Use
mex filename.c -v -l*libraryname*.lib
. The -l switch indicates to the compiler that you are specifying a library that you want to include. If this library is not found, I would include the full path to the library in the command. I hope that provides you with some assistance. Using this methodology has been successful for me.