问题描述
我整天都在摆弄,所以我想我可以让每个人都从我的经验中受益,请在下面查看我的答案.
I fiddled with this the whole day, so I thought I might make everyone benefit from my experience, please see my answer below.
我首先遇到了在Matlab中运行已编译的Mex文件的问题,因为Matlab抱怨说它无法打开共享库libarmadillo
.我使用环境变量LD_LIBRARY_PATH
解决了此问题和LD_RUN_PATH
(在osx中为DYLD_LIBRARY_PATH
和LYLD_RUN_PATH
).
I first had a problem with running a compiled Mex file within Matlab, because Matlab complained that it couldn't open the shared library libarmadillo
. I solved this using the environment variables LD_LIBRARY_PATH
and LD_RUN_PATH
(DYLD_LIBRARY_PATH
and LYLD_RUN_PATH
in osx).
问题仍然存在,即使是完全相同的代码也可以在Matlab外部编译并运行良好(不是Mex'd),一个简单的测试文件仍会在运行时出现段错误.
The problem remained however, that a simple test file would segfault at runtime even though the exact same code would compile and run fine outside Matlab (not Mex'd).
推荐答案
Segfault似乎是由于Matlab在捆绑的LAPACK和BLAS库中使用64位整数(long long
或int64_t
)造成的.另一方面,犰狳默认使用32位整数(在64位平台上为常规int
或int32_t
).
The segfault seems to be caused by the fact that Matlab uses 64bits integers (long long
or int64_t
) in its bundled LAPACK and BLAS libraries. Armadillo on the other hand, uses 32bits integers (regular int
on a 64bits platform, or int32_t
) by default.
有两种解决方案;第一个涉及强制Matlab改为链接到系统的库(使用int
),第二个涉及更改Armadillo的配置文件以通过BLAS启用long long
.我倾向于认为第一个更可靠,因为没有黑盒效应,但是它也更麻烦,因为您需要手动安装并记住BLAS和LAPACK库的路径.
There are two solutions; the first one involves forcing Matlab to link to the system's libraries instead (which use int
s), the second involves changing Armadillo's config file to enable long long
s with BLAS. I tend to think that the first is more reliable, because there is no black-box effect, but it's also more troublesome, because you need to manually install and remember the path of your BLAS and LAPACK libs.
两个解决方案都要求我停止使用Armadillo的共享库,并手动链接/包含源.为此,您只需在系统上安装LAPACK和BLAS(如果尚未安装,在libblas-dev
和liblapack-dev
的Ubuntu中),然后将整个includes
目录复制到类似$HOME/.local/arma
的合理位置即可.例子.
Both solutions required that I stopped using Armadillo's shared libraries and linked/included manually the sources.To do this, you must simply install LAPACK and BLAS on your system (if they are not already there, in Ubuntu that's libblas-dev
and liblapack-dev
), and copy the entire includes
directory somewhere sensible like in $HOME/.local/arma
for example.
在matlab控制台中,将环境变量BLAS_VERSION
和LAPACK_VERSION
设置为指向系统的库.就我而言(Ubuntu 14.04,Matlab R2014b):
From the matlab console, set the environment variables BLAS_VERSION
and LAPACK_VERSION
to point to your system's libraries. In my case (Ubuntu 14.04, Matlab R2014b):
setenv('BLAS_VERSION','/usr/lib/libblas.so');
setenv('LAPACK_VERSION','/usr/lib/liblapack.so');
然后可以正常编译:
mex -compatibleArrayDims -outdir +mx -L/home/john/.local/arma -llapack -lblas -I/home/john/.local/arma test_arma.cpp
,或者如果您在includes/armadillo_bits/config.hpp
中定义标志ARMA_64BIT_WORD
,则可以删除选项-compatibleArrayDims
.
or if you define the flag ARMA_64BIT_WORD
in includes/armadillo_bits/config.hpp
, you can drop the option -compatibleArrayDims
.
第二种解决方案涉及取消Armadillo的配置文件includes/armadillo_bits/config.hpp
中的标志ARMA_BLAS_LONG_LONG
的注释. Matlab将链接到其捆绑的LAPACK和BLAS库,但是这次Armadillo不会出现段错误,因为它使用了正确的字长.与以前一样,如果要删除-compatibleArrayDims
,也可以取消注释ARMA_64BIT_WORD
.
The second solution involves uncommenting the flag ARMA_BLAS_LONG_LONG
in Armadillo's config file includes/armadillo_bits/config.hpp
. Matlab will link to its bundled LAPACK and BLAS libraries, but this time Armadillo won't segfault because it's using the right word-size. Same than before, you can also uncomment ARMA_64BIT_WORD
if you want to drop the -compatibleArrayDims
.
这篇关于犰狳+ Matlab Mex segfault的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!