Linking mex with Lapack

4 views (last 30 days)
Raunak Borker
Raunak Borker on 16 Feb 2016
Commented: Walter Roberson on 18 Feb 2016
Hi,
I have a small test code (mex function, 'test_lapack.cpp') that solves a linear system. To do this it calls another function written as a separate C++ file ('solve.cpp'). The C++ file calls the dgesv_ function of the lapack library. On my mac with MATLAB R2015a, I compile these as:
mex test_lapack.cpp solve.cpp -llapack
and it compiles and I can use the resulting function in MATLAB. When I do a similar thing on linux (centOS) with MATLAB R2013a:
mex test_lapack.cpp solve.cpp -lliblapack.so.3.2.1
the code still compiles. But when I call the resulting function from MATLAB, MATLAB crashes giving the following error:
INTEL MKL ERROR: parameter 2 was incorrect on entry to DGESV .
I am not sure why the Intel MKL version of lapack is being used even though I explicitly linked it to the non-intel library and the compiler is g++ (not the intel one).
I would appreciate any help in resolving this.
Thanks

Accepted Answer

Walter Roberson
Walter Roberson on 17 Feb 2016
-lliblapack.so.3.2.1 is not a specific link against liblapack.so.3.2.1 . Instead, it adds liblapack.so.3.2.1 to the list of libraries to link against to resolve symbols. There is still a library order, and earlier libraries in the order may have resolved the symbol. Especially since you are using a .so instead of requesting a static library link.
  5 Comments
Raunak Borker
Raunak Borker on 18 Feb 2016
So it's not ideal to use LD_PRELOAD. Or atleast the way I am using it. Although my code runs with this fix, MATLAB seg faults for example while doing a least-squares fit using the basic fitting tool in plot.
Walter Roberson
Walter Roberson on 18 Feb 2016
I would think it more likely that the approach would be closer to
old_ld = getenv('LD_LIBRARY_PATH');
setenv('LD_LIBRARY_PATH', ['/usr/lib64/liblapack.so.3:', old_ld])

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!