How do I compile a MEX file using Armadillo?

13 次查看(过去 30 天)
I am trying to compile a MEX file using C++ code which uses the Armadillo C++ library.  Armadillo depends on BLAS and LAPACK, both of which ship with MATLAB. Linking against custom copies of libraries is not a supported workflow, so instead I am linking against the versions within MATLAB using a "mex" command like:
>> mex(..., ['-L' matlabroot '\extern\lib\win64\mingw64'], '-llapack', '-lblas')
Or, if using the MSVC C++ compiler rather than MinGW:
>> mex(..., ['-L' matlabroot '\extern\lib\win64\microsoft'], '-llapack', '-lblas')
Running these gives outputs a large number of errors, all of the same pattern:
%USER%\Local\Temp\mex_10327564389625623_6476\UCompC.obj:UCompC.cpp:(.text+0x113f8): undefined reference to `dgemv_'
%USER%\Local\Temp\mex_10327564389625623_6476\UCompC.obj:UCompC.cpp:(.text+0x11ba5): undefined reference to `dnrm2_'
%USER%\Local\Temp\mex_10327564389625623_6476\UCompC.obj:UCompC.cpp:(.text+0x13fc9): undefined reference to `dgemv_'
%USER%\Local\Temp\mex_10327564389625623_6476\UCompC.obj:UCompC.cpp:(.text+0x160a7): undefined reference to `dgemv_'
%USER%\Local\Temp\mex_10327564389625623_6476\UCompC.obj:UCompC.cpp:(.text+0x2f7a0): undefined reference to `ddot_'
%USER%\Local\Temp\mex_10327564389625623_6476\UCompC.obj:UCompC.cpp:(.text+0x3efe6): undefined reference to `dgemv_'
%USER%\Local\Temp\mex_10327564389625623_6476\UCompC.obj:UCompC.cpp:(.text+0x3f61a): undefined reference to `dgemv_'
%USER%\Local\Temp\mex_10327564389625623_6476\UCompC.obj:UCompC.cpp:(.text+0x44831): undefined reference to `dgemv_'
%USER%\Local\Temp\mex_10327564389625623_6476\UCompC.obj:UCompC.cpp:(.text+0x44add): undefined reference to `dgemm_'
%USER%\Local\Temp\mex_10327564389625623_6476\UCompC.obj:UCompC.cpp:(.text+0x44c84): undefined reference to `dgemv_'
...
All of these are functions in BLAS and LAPACK which are referenced by Armadillo.
How do I get Armadillo to compile? I am using MATLAB release R2022a.

采纳的回答

MathWorks Support Team
The underlying issue is that the copies of BLAS and LAPACK that ship with MATLAB define the functions without an underscore (e.g. "dgemv"), whereas Armadillo refers to those same functions with an underscore (e.g. "dgemv_").
To make Armadillo reference the right functions, follow the following steps:
1) Create a C++ header file and use it to redefine the function names with underscores to the equivalent function name without an underscore, like the following:
#define dsyevd_ dsyevd
#define dsyev_ dsyev
#define dgemv_ dgemv
#define dgemm_ dgemm
...
Save the header file. The header file can be found attached as "defines.hpp".
2) Add an '#include "defines.hpp"' line above any #include for Armadillo. In essence, any line like:
#include "armaMex.hpp"
should become:
#include "defines.hpp"
#include "armaMex.hpp"
3) Add the current directory (which is the one containing "defines.hpp") to the compilation by adding '-I.' to the "mex" command:
>> mex(..., '-I.', ['-L' matlabroot '\extern\lib\win64\microsoft'], '-llapack', '-lblas')

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Call C++ from MATLAB 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by