Cannot compile mex file on Windows - matlab

When I attempt to compile a .mex file on Windows, I get the following error. How can I fix this?
Error using mex
MEX cannot find library 'mwblas' specified with the -l option.
MEX looks for a file with one of the names:
libmwblas.lib
mwblas.lib
Please specify the path to this library with the -L option

As the error message states, you need to specify the directory in which the .lib file is located using the -L option or specify the full path to the .lib file as an input to mex. This library is located in matlabroot/extern/lib. So you could do something like
blas = fullfile(matlabroot,'extern','lib',computer('arch'),'microsoft', 'libmwblas.lib');
mex('-largeArrayDims', 'mymexfile.cpp', blas)
Or you can specify the folder with -L
libdir = fullfile(matlabroot,'extern','lib',computer('arch'),'microsoft');
mex('-largeArrayDims', ['-L', libdir], '-lmwblas', 'mymexfile.cpp', )
More info about compiling mex files to use LAPACK and BLAS is here

Related

Link to gfortran for mex file

I am trying to create mex files for using some C++ code in Matlab, but running into an error with the compilation which, I guess, goes beyond Matlab. I am on Windows.
The example I'm struggling with is the Mex Interface example in the Armadillo library for linear algebra, available: here. The Matlab file is what I'm running and it looks like this:
% Compile the demo as a mex file
mex -lgfortran armaMex_demo.cpp -I/path/to/armadillo
The error I get is:
Error using mex
MEX cannot find library 'gfortran' specified with the -l option.
MEX looks for a file with one of the names:
libgfortran.lib
gfortran.lib
Please specify the path to this library with the -L option.
Trying some things I found online, I installed MinGW and cygwin64 and added the paths to their bin folders to my PATH variable. Before that I also installed MinGW via Matlab.
My question: How do I get it to link with gfortran?
If I search C:for libgfortran I find some files, but none of them have the file ending .lib (but e.g. .dll and .a) and if I search for gfortranI find some .exe files but again, no .lib file. If I run gfortran -dumpversion in the terminal, I get 4.9.3 back, so it is obviously there somewhere. I am obviously missing something, and this is really not my forte, to say the least.

Can not build a mex file in octave using mkoctfile --mex

I am trying to build a mex file in octave. The Octave instructions says that we need to use mkoctfile --mex to build one. The file I am trying to build is in the following path:
> C:\devwork\Octave\boosted\toolbox\channels\private\rgbConvert.cpp
When I write
> mkoctfile --mex rgbConvert.cpp
I get the following error:
> g++: error: rgbConvert.cpp: No such file or directory g++: fatal
> error: no input files compilation terminated.
Note: My current directory is set correct. It is at \devwork\Octave\boosted\toolbox\channels\private
Can some one explain me why am I getting this error.
The problem is resolved using: mkoctfile --mex -DMATLAB_MEX_FILE rgbConvertMex.cpp
Hence, if you want to convert any Matlab compiled .mex file to Octave executable file, do the following two steps:
Modify wrappers.hpp file by adding #include <stdlib.h>
Save the file.
Simply run: mkoctfile --mex -DMATLAB_MEX_FILE file_name
.o file will be created.
I did it using the same and it worked fine.
Eskapp was grateful enough to release the Octave compiled MEX files. You'll need to manually add the files to the main toolbox.Please find the link here:
https://github.com/Eskapp/Piotr_vision_extrafiles_Octave
Hope it helps others :)

Issues in compiling a mex file

I am having some issues in compiling using mex this software http://www.cmap.polytechnique.fr/~aspremon/ZIP/COVSEL.zip. When I use mex and give it the files to compile it shows the following error
error: 'CblasColMajor' undeclared (first use in this function)
I am compiling the mex file from matlab and in Mac LionOS. Any suggestions
I am using the following code to compile
mex BoxQP.c BoxQP_mex.c utils.c
Make sure the MACVERSION macro is defined, or define it yourself. It looks like the CblasColMajor enum is defined in the cblas.h file which is included on line 18 of BoxQP.h. So I'm guessing that you either need to install the cblas library or it's there by default on OSX. Here's what I had to use to compile it on Win32:
mex -v -g BoxQP_mex.c BoxQP.c utils.c -LC:\MATLAB\R2009bSP1\extern\lib\win32\microsoft -lmwblas
HTH, might want to include the line you're using to compile it if you can't figure it out.
EDIT
You'll need to find the library path for the Matlab install and then under that directory you'll need to look for extern\lib\ then look for your platform and library type like, extern\lib\linux\maxos I'm not sure what it will look like, but that's a guess. also, include the Mathworks BLAS library using, -lmwblas.

mex compiler could not find include path in .h files

Hi I'm using MEX in Matlab.
The c code uses boost and a lot of headers are included.
And at first my problem is like this
>>mex readTrackletsMex.cpp
readTrackletsMex.cpp
e:\kitti\tools\devkit\matlab\tracklets.h(7) : fatal error C1083: can not open include file:“boost/serialization/nvp.hpp”: No such file or directory
This is because the include path is not specified. So I copied the headers into "/include/boost/XX" and added "-I" flag.
This time it seems that the compiler cannot find the include path in one of the header file.
>> mex -Iinclude readTrackletsMex.cpp
readTrackletsMex.cpp
include\boost/serialization/nvp.hpp(21) : fatal error C1083: can not open include file:“boost/config.hpp”: No such file or directory
Any idea on this? How do I specify a "global" include path for the compiler?
Thanks for your time.
You could try adding the include paths to the environment variables C_INCLUDE_PATH and CPLUS_INCLUDE_PATH. The other issue may be that you put in -Iinclude, and not -I/include

MATLAB include error : fatal error C1083: Cannot open include file: 'cblas.h'

I am using a optimization toolbox for solving sparse estimation problems. I have downloaded the software from SPAMS homepage and would like to create a mex-file from mexLasso.m for my application.
I transfer all the error-invoking include files to MATLAB external/include folder. In MATLAB, once I type in:
>> mex mexLasso.cpp
I receive this error:
C:\Program Files\MATLAB\R2012b\extern\include\utils.h(28)
fatal error C1083: Cannot open include file: 'cblas.h'
How can I solve this problem?
Thanks
For mex-ing: Have you setup your compiler?
>> mex -setup
Choose visual studio compiler over Matlab's lcc.
Do you have a header file cblas.h? where is it located?
Use the following flags to compile
>> mex -O -largeArrayDims -v mexLasso.cpp
The -v flag should give you a verbose output - see what are the include directories the compiler search in for header file. You can add folders to the search path with the flag -I <folder name>