I am trying to add a simple library to matlab using the "loadlibrary" function. I first try linking the gcc compiler to matlab with mex -setup and get this:
The options files available for mex are:
1: /Applications/MATLAB_R2012a.app/bin/mexopts.sh :
Template Options file for building gcc MEX-files
0: Exit with no changes
So I just chose 1 and continued. I then received this message:
Warning: The MATLAB C and Fortran API has changed to support MATLAB
variables with more than 2^32-1 elements. In the near future
you will be required to update your code to utilize the new
API. You can find more information about this at:
http://www.mathworks.com/help/techdoc/matlab_external/bsflnue-1.html
Building with the -largeArrayDims option enables the new API.
after this i type in:
loadlibrary('Samplelib.dylib','Samplelib.h')
Error using loadlibrary (line 419)
Failed to preprocess the input file.
Output from preprocessor is:/bin/bash: gcc-4.2: command not found
Any ideas why this is happening?
You are using matlab on mac, true? You need to install gcc first, before you will be able to proceed. Try Xcode - you need gcc-4.2 because it is hard-coded in the mentioned mexopts.sh How to install it depends on your MacOS version, but google xcode, you will find plenty of links.
The first "Warning" is just to tell you that the C API is now better than ever because it supports a huge number of elements, but will need to be enabled using a new option in later versions of Matlab. Long story short, you don't care. The next error about gcc-4.2 means that the gcc 4.2 compiler is not on your path. If you do a:
[s1,r1] = system('which gcc')
disp( r1 )
disp( s1 )
[s2,r2] = system('which gcc-4.2')
disp( r2 )
disp( s2 )
Likely, one or both will come back with an error. Make sure the gcc compiler is on your PATH environment variable.
Like againor says, you'll need to have the compiler installed as well. :-)
Related
I'm trying to use matlab_get_all_valid_matlab_roots_from_registry() from FindMatlab.cmake and print the results using this script:
cmake_minimum_required(VERSION 2.8)
find_package(Matlab REQUIRED)
matlab_get_all_valid_matlab_roots_from_registry(a b)
message("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
message(STATUS " !!!!! ${a} ${b} ${Matlab_ROOT_DIR} ####")
message("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
Is it necessary that I first call find_package() before calling matlab_get_all_valid_matlab_roots_from_registry() ?
The output of the script doesn't print the contents of the variables a and b that are return values of that function. This is the output:
1> Command line: c:\program files (x86)\microsoft visual studio\2017
\community\common7\ide\commonextensions\microsoft\cmake\CMake\bin\cmake.exe -G "Ninja" -DCMAKE_INSTALL_PREFIX:PATH="C:\Users\AppTeam\CMakeBuilds\6f0f93b4-4e73-e838-98c8-2bfd807d82bf\install\x64-Debug (default)" -DCMAKE_CXX_COMPILER="C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/cl.exe" -DCMAKE_C_COMPILER="C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/cl.exe" -DCMAKE_BUILD_TYPE="Debug" -DCMAKE_MAKE_PROGRAM="c:\program files (x86)\microsoft visual studio\2017\community\common7\ide\commonextensions\microsoft\cmake\Ninja\ninja.exe" "C:\Users\AppTeam\Documents\bil\matlab\codegen\lib\mcadd"
1> Working directory: C:\Users\AppTeam\CMakeBuilds\6f0f93b4-4e73-e838-98c8-2bfd807d82bf\build\x64-Debug (default)
1> -- !!!!! C:/Program Files/MATLAB/R2018b ####
1> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1> -- Configuring done
It seems that Matlab_ROOT_DIR variable is OK, but I would like to find all MATLAB installations by using matlab_get_all_valid_matlab_roots_from_registry() but the output variables are not displayed. What am I doing wrong?
The proper syntax is:
matlab_get_all_valid_matlab_roots_from_registry(matlab_versions, matlab_roots)
where matlab_versions is an input argument, and should contain a list of the MATLAB versions you want the roots for. Since you pass an empty list as input, you get zero roots back.
According to the sources, matlab_versions comes either from extract_matlab_versions_from_registry_brute_force or matlab_extract_all_installed_versions_from_registry. The former is an internal macro, and the latter is a function available only on Windows. So on Windows you can do:
find_package(Matlab REQUIRED)
set(matlab_versions)
matlab_extract_all_installed_versions_from_registry(ON matlab_versions)
matlab_get_all_valid_matlab_roots_from_registry(matlab_versions, matlab_roots)
message(STATUS " !!!!! ${matlab_versions} ${matlab_roots} ${Matlab_ROOT_DIR} ####")
Also, yes, you do need to issue find_package(Matlab) to make the functions defined therein available.
Note regarding cmake_minimum_required(VERSION 2.8):
If a user has a version of CMake that old (2.8 was released in 2009!), you might find that the user's MATLAB will not be found. The script has a list of known versions of MATLAB to look for, so you need to always use the newest version of the script to find the newest versions of MATLAB (or explicitly add information about the release you are using). The version of the FindMatlab.cmake script in 2009 explicitly looked for MATLAB 7.0 and 7.0 SP1, which were released in 2004, already 5 years outdated at that time, and didn't have the possibility to look for newer versions of MATLAB. This script also did not define a function matlab_get_all_valid_matlab_roots_from_registry.
So, in general, I recommend that you look for the CMake features you are using, and require a version of CMake that supports those features. I know it is common to see "require 2.8" in CMake scripts, but this is not always the best choice. In the case of MATLAB, it certainly is not the best choice.
I have a script in matlab that calls other libraries. I use matlab version 2012a on linux . I get below error and I don't know how to fix it.
The error is :
Invalid MEX-file '/home/XXX/nearest_neighbors.mexa64':
libflann.so.1.8: cannot open shared object file: No such file or
directory
Error in flann_search (line 82)
[indices,dists] = nearest_neighbors('find_nn', data, testset, n, params);
Error in MyScript (line 73)
[nresult, ndists] = flann_search(Ntraindata', Ntraindata', resu.KNN, struct('algorithm','composite',...
That library you are referring to - https://github.com/mariusmuja/flann/ - has the nearest_neighbors function written in MEX code. MEX code is C code that is used to interface with MATLAB. People usually write heavily computationally burdening code over in MEX as it is known to handle loops and other things faster. The inputs come from MATLAB and are sent to this MEX function, and the outputs come from the MEX function and are piped back to MATLAB. It's basically a nice black box where you can use it just like any other MATLAB function. In fact, a lot of the functions that come shipped with MATLAB have MEX wrappers written to promote acceleration.
You are getting that error because you need to compile the nearest_neighbors function so that there is a MEX wrapper that can be called in MATLAB. That wrapper is missing because you haven't compiled the code.
First, you need to set up MEX. Make sure you have a compiler that is compatible with your version of MATLAB. You can do that by visiting this link:
http://www.mathworks.com/support/compilers/R20xxy/index.html
xx is the version number that belongs to your MATLAB and y is the character code that comes after it. For example, if you are using R2013a, you would visit:
http://www.mathworks.com/support/compilers/R2013a/index.html
Once you're there, go to your Operating System and ensure you have one of those supported compilers installed. Once you have that installed, go into MATLAB, and in the command prompt, type in:
mex -setup
This will allow you to set up MEX and choose the compiler you want. Given your error, you're using Linux 64-bit, so it should be very easy for you to get GCC. Just make sure that you choose a version of GCC that is compatible with your version of MATLAB. Once you choose the compiler, you can compile the code by doing this in the command prompt:
>> mex -v -O nearest_neighbors.cpp
This should generate the nearest_neighbors MEX file for you. Once that's done, you can now run the code.
For more detailed instructions, check out FLANN's user manual - http://people.cs.ubc.ca/~mariusm/uploads/FLANN/flann_manual-1.8.4.pdf - It tells you how to build and compile it for MATLAB use.
Good luck!
I am trying to compile a CUDA program using MEX within Matlab2014a and can't get it to work.
I installed the newest CUDA toolbox (6.5) and driver (340.62) and the samples work correctly, so I guess it is correctly installed.
I also installed MS Visual Studio 2012 Professional and Matlab 2014a.
I followed the exact description in the Matlab Help in "GPU Computing -> Examples and How To -> Run MEX-Functions Containing CUDA Code".
My CUDA file ends with .cu (so I am trying to compile Matlab's standard example mexGPUExample.cu, but I get the same error with other .cu files), the mex_CUDA_win64.xml is in the same folder, the environment variable MW_NVCC_PATH is set correctly in the user variables and just to be sure I also called "setenv('MW_NVCC_PATH,...)" in Matlab.
If I try to compile a CUDA example with
mex -largeArrayDims mexGPUExample.cu
I get the following error:
cl : Command line warning D9024 : unrecognized source file type 'mexGPUExample.cu', object file assumed
cl : Command line warning D9027 : source file 'mexGPUExample.cu' ignored
cl : Command line warning D9021 : no action performed
D:\PROGRAMS\MATLAB~1\BIN\MEX.PL: Error: Compile of 'mexGPUExample.cu' failed.
Unable to complete successfully.
I also tried to set the compiler correctly using
mex -setup
and chose the MS Visual Studio Compiler.
The code itself works because I tested it on other systems.
I have no idea what I am doing wrong.
Any help is appreciated.
Matlab only supports CUDA 5.5 on R2014a, so your CUDA 6.5 by default cannot compile mex files under Matlab. Matlab R2014a supports VS 2012 now, but you need to verify you have VS 64-bit compiler if your Matlab is 64-bit. You can compile a normal .mex file with .cpp source file, (lots of these files under matlabroot/extern folder) to see whether Matlab works well with your VS.
Note that Matlab has a lag in supporting the latest compilers, both VS/gcc and Cuda. It is always good choice to check the compiler requirement by Matlab, before using the latest compilers.
Also check whether your Cuda kit and graphics drivers are correctly installed and work seamlessly with VS. You can compile and run some .cu files under the VS environment.
If you have finished all above and changed to the correct version of compilers, follow the instructions on matheworks website to compile the mexGPUExample file. If my memory is correct, you need to set environment variables and copy the XML file to the .mex file path. Then it should work.
I have created a mex function (more specifically, using CUDA)
the compilation was successful, and I got a mex file zMul.mexmaci64
but on the execution, matlab reported an error:
Invalid MEX-file '/Users/zlw/Documents/MATLAB/lowComplexity/cbased/matMulGPU/zMul.mexmaci64':
dlopen(/Users/zlw/Documents/MATLAB/lowComplexity/cbased/matMulGPU/zMul.mexmaci64, 1):
Library not loaded: #rpath/libcublas.6.0.dylib
Referenced from: /Users/zlw/Documents/MATLAB/lowComplexity/cbased/matMulGPU/zMul.mexmaci64
Reason: image not found
What should I do do solve it?
additional info
setting the environment vars (PATH,LD_LIBRARY_PATH,DYLD_LIBRARY_PATH) in Matlab and in .bash_profile doesn't work for me
I'm pretty sure that the environment vars are set correctly because when I created an alias to the dylib file, Matlab detected it, tried to load it, but failed with message:no suitable image found
Thanks!
Use otool -L in both Matlab and UNIX console.
In Matlab:
!otool -L /path/to/zMul.mexmaci64
In UNIX console:
otool -L /path/to/zMul.mexmaci64
Try to find the difference between them. If there is a difference in dependency, that is probably breaking the MEX binary. You might need to apply the same technique for the dependent dylib objects recursively. Typically, enforcing the one appearing in UNIX console using DYLD_INSERT_LIBRARIES solves the problem.
Another possibility is the C++ runtime compatibility. If you're using OS X Mavericks, you should check that your MEX command is using libc++ but not libstdc++ in mexopts.sh. Below is my configuration example in mexopts.sh:
CC='clang'
CXX='clang++'
SDKROOT='/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/'
MACOSX_DEPLOYMENT_TARGET='10.9'
CFLAGS="$CFLAGS -Dchar16_t=uint16_t"
CXXFLAGS="$CXXFLAGS -std=c++11 -stdlib=libc++ -DCHAR16_T"
CXXLIBS="$MLIBS -lc++"
This post might help: http://www.seaandsailor.com/matlab-xcode6.html
It was easier than I thought. Just replace all 10.x with your OS X version and add -Dchar16_t=UINT16_T to CLIBS in mexopts.sh file.
I am using Windows XP and matlab version is 7.10.0.
I have the levmar(Levenberg Marquardt) package from http://www.ics.forth.gr/~lourakis/levmar/levmar-2.5.tgz
In the README file, we are told to compile in matlab using mex using the following command:
mex -DHAVE_LAPACK -I.. -O -L -L levmar.c -llevmar -lclapack -lblas -lf2c.
I downloaded lapack.lib , blas.lib and f2c.lib for windows
UPDATE:
The original error got resolved after I built a vc project file given in the package.
But now there are some error messages like :
levmar.lib(misc.obj) : error LNK2019: unresolved external symbol _dgemm_ referenced in function _dlevmar_trans_mat_mat_mult
Did you create a file with a mex-function gateway? You can't just compile a c-function for Matlab; you need to do a little bit of work to take care of the I/O between Matlab and the c-code.
If you follow the steps outlined in this document, you should do fine.
You may have a look at immoptibox, which comprises Levenberg-Marquardt algorithm as well.
I just figured it out after searching a while and noticed that the levmar package included a vc project file which i needed to build and it created a file called levmar.lib .
But now I am getting some errors which involves messages like 'unable to resolve external symbols'