I have a MATLAB script that I have to compile for deployment. I'm using the command 'mcc', however, I also need to include the toolboxes: PRTools (http://37steps.com/software/) and dd_tools (http://prlab.tudelft.nl/david-tax/dd_tools.html). I'm trying to include using the command addpath(), it does not work. I have no idea how to include those toolboxes. I tried to use:
if ~isdeployed
addpath PRTools
addpath dd_tools
end
but it does not work as well.
For deployment with mcc, you should add the paths to your MATLAB path (using pathtool or addpath) before compiling the application rather than calling addpath from within your code.
mcc statically analyzes your code to determine what needs to be included in the executable. Part of this static code analysis includes finding all function calls within your code and locating the corresponding source files. Since you are calling addpath from within your code, MATLAB is unable to properly locate all of the toolbox functions (since they are added to the path dynamically at run-time and the code is analyzed statically) and will omit these files.
If you add them to your MATLAB path first, mcc will be able to statically analyze your code, locate all needed toolbox functions, and include them in the resulting executable.
A cleaner alternative to modifying your path is to use the -I option when calling mcc to specify a specific folder to include.
-I Add a new folder path to the list of included folders. Each -I option adds a folder to the beginning of the list of paths to search. For example,
-I <directory1> -I <directory2>
sets up the search path so that directory1 is searched first for MATLAB files, followed by directory2. This option is important for standalone compilation where the MATLAB path is not available.
For your case this would be something like
mcc -m main.m -I PRTools -I dd_tools
Related
We're using the latest version of CMake(3.17), trying to compile a top level .m file into an executable on Matlab 2015b
I've done
find_package(Matlab OPTIONAL_COMPONENTS MEX_COMPILER MCC_COMPILER)
and it finds matlab. I'm able to make MEX files using matlab_add_mex() (and access them from matlab), but the next step is to take the top level .m file and compile it to an executable so it can be sent off to a compute farm to run very large input vectors.
At the commandline, we just do mcc -m simulation.m -I <required include paths> etc and it works. I want this to work in our CMake build environment with dependencies properly calculated (as best I can)
I assume I need to use add_custom_command() like what is mentioned here (https://gitlab.kitware.com/cmake/cmake/issues/18183), and I assume I need to calculate the dependencies correctly.
Using this cme.exe line, I can get the dependencies out to a file:
matlab -wait -nosplash -nojvm -nodesktop -r "[fList,~] = matlab.codetools.requiredFilesAndProducts('simulation.m');;for file = fList;fprintf(2,'%s\n',file{:});end;exit" -logfile output.txt
Is there a better way to do this? This ends up launching matlab and blopping the output to a file, rather than stdout or whatever. It's pretty dang slow to start up, as well.
I'm contemplating writing a python script or C program that embeds matlab that gets the information then prints it to stdout or just using mcc on the script that is supposed to calculate the dependencies, but that seems really, really convoluted for something that seems like a standard step in any repeatable build process.
So, any pointers? I can't find a way to get the dependencies except inside a matlab command line, and I'd hope that CMake had m->exe (in addition to mex) in its wheelhouse.
fwiw, I took the following and put it into a file get_dependencies.m:
function print_dependencies(filename)
[fList,~] = matlab.codetools.requiredFilesAndProducts(filename)
for file = fList
fprintf(1,'%s\n',file{:})
end
end
and compiled it using
mcc -m get_dependencies.m
And when it runs, it whines about undefined variables:
Undefined variable "matlab" or class "matlab.codetools.requiredFilesAndProducts".
I am trying to compile a Matlab function using the mcc function. My function writes a line of text to a file.
function hello()
ofid = fopen('hello.txt','w');
fprintf(ofid, 'Hello there, this is matlab.\n');
fclose(ofid);
end
It runs as expected when called from the Matlab console. However, when I compile the file to an executable using mcc
/opt/matlab/matlab2008a/bin/mcc -m hello.m
and run it as
./hello
It seems to run forever and produces no hello.txt file. Both the executable as well as the Matlab function files are in the Matlab working directory.
I am unable to see what can be possibly wrong in my use of mcc. Please help.
Typically, you need to setup the MATLAB runtime environment when executing mcc compiled applications.
You'll probably get more mileage the first time by using deploytool (from the command line) to compile your hello.m program here. MATLAB's application builder will produce a run_hello.sh file that you can peruse through to see how they setup their libraries. Then, to execute your program you would actually call it like this:
./run_hello.sh /Applications/MATLAB/MATLAB_Runtime/v90/
Note: I have just included a generic location for the MATLAB runtime environment, but the actual location will vary depending on your platform and version of MATLAB you built the application with.
can you give me a basic c++ code that inclue opencv libraries , and can call the matlab engine ?
the file should compile using matlab mex.
(my problem is when I use the engine and compile it with "mex -f optionfile file.cpp"I get error message "cannot include opencv\cv.h" , but using the opencv without the engine its work using the mex file.xpp)
You need Matlab engine if you want to run .m files outside of Matlab command window (e.g. bash, c++ source file, etc.)
In your case, mex program (Matlab External Interface which acts like g++ or other c++ compilers) is required to compile a c++ source file. You must provide mex program a source file which is written according to what Matlab has introduced in its documentation.
If your source code (.cpp file) is including opencv/cv.h file and using one of its methods, you need to link the appropriate library (say libopencv.so, libopencv.dll or etc,) using -l flag. (I use mex foo.cpp -l opencv_core)
I hope it helps, you can provide your source
I am trying to execute some example code from a MATLAB toolkit, 'oscmex'. This toolkit allows for communication using the OSC protocol over MATLAB. I presume this question is non-specific; it should apply to any toolkit that is set-up in the manner that this one is.
Reasons aside, I'm having some simple trouble getting the toolkit up and running. The toolkit comes with no documentation whatsoever; just a set of six DLL files (in one directory), and a set of four MATLAB '.m' example code files (in another directory). Every toolkit I've used in the past has either been a built-in kit or has had an intuitive (semi-automated) install procedure.
After downloading the toolkit, the first thing I tried was to simply run one of the '.M' example codes. This failed as the first line of the code contained the function osc(), which is not (currently) recognised by MATLAB.
So, I figured maybe I need to move the '.M' files into the same folder as the DLLs; perhaps MATLAB would see the functions inside the DLLs. No dice.
So, I realise that I have to somehow link MATLAB to the DLLs on startup. I tried adding the DLLs to a folder and adding an entry to that in the 'pathdef.m' file. This also failed.
I've read somewhere I can load a DLL file by using the loadlibrary() function. So, I tried doing this for the DLL files. This failed on the first file:
>> loadlibrary('osc_free_address.dll')
Error using loadlibrary>lFullPath (line 587)
Could not find file osc_free_address.h.
I'm starting to run out of options... How can I get this set of DLLs up and running?
Browsing this library's web page it would seems these DLLs are just old form of mex files.
Therefore, they should not be used in the context of shared library (e.g., using loadlibrary and calllib), but rather compiled directly to mex files.
To do so, I would suggest the following steps:
Make sure you have a working mex compiler configured for your Matlab.
In matlab, type:
>> mex -setup
this will guide you through the configuration process. I understand that you are working on a windows machine, I usually work with visual studio compiler - works best for me.
This library's README file suggests that OSC
requires liblo 0.22 or later. See http://plugin.org.uk/liblo/
Make sure you have this library and it is located in you LD_LIBRARY_PATH (see e.g., this question for details, or the mex docs).
Get the source code for OSC library from their repository.
Compile the sources in matlab using
>> mex -O -largeArrayDims osc_free_address.c
>> mex -O -largeArrayDims osc_free_server.c
and so on for all 7 c source files. After mex-ing the c files you'll have mex files that you can run from Matlab as if they were regular functions.
You may find it useful to use the library's make file, as suggested by Andrew Mao.
Good luck,
If you look at the build for that software, it is compiling mex files, not DLLs (shared libraries): http://sourceforge.net/p/oscmex/code/4/tree/trunk/src/osc_make.m.
I would try using the mex commands instead of the dll commands (perhaps the files are just misnamed.) Even better, I would compile the files yourself with mex using the build file in source.
Note that the instructions also say that you need liblo-0.22 in order to run the library, so make sure you have that accessible as well.
I took a look at your OSC Toolkit. It seems they have been compiled by MATLAB mex. But, it is not mentioned for which kind of architecture they have been built. You can type mexext at MATLAB command prompt to find the extension for your MATLAB mex files. Then, change the DLL extensions to the given extension. If the original mex is compatible with your matlab, the DLL can be easily accessed by MATLAB. Just make sure to add the folder to your MATLAB path.
Try changing the extension from .dll to .mexw32 (in win32), or .wexw64 (in win64). It's a long shot but it might work.
The Shared Libraries cannot be used directly. As you have mentioned, you need to load them into MATLAB using loadlibrary. According to the documentation, loadlibrary takes two arguments (at least). The first argument is the name of the file, and the second one is the header file which contains definition of functions and external variables. If you do not provide the header file, the MATLAB looks for the a file with the same name as the DLL. Having said that, you need to have access to the header file or at least if you know how the function looks like, you need to write a header for the DLL.
I have worked with the DLLs in MATLAB. The MATLAB is not very user-friendly as long as DLL is concerned. Especially, if the DLL is written in a language other than C (or C++) you will have trouble loading the function into MATLAB.
Besides, MATLAB can only support some specific DLLs. Based, on your version of MATLAB, you need to find out whether or not the shared library is supported by MATLAB. Have a look at Here
In a nutshell, it is not easy to load a DLL into MATLAB. You need to have some information from DLL.
I've created a deployment project which works rather well and now I want to add it to source control repository for others to use.
The main problem I'm facing is that the .prj file which deploytool creates contains absolute paths which will not work on other computers. So far I've tried the following:
Create the stand alone exe using just mcc without deploytool. This works great but I could find a way to create the final _pkg.exe which contains everything. mcc doesn't seem to be able to create this file and there doesn't seem to be any other tool which does. Is this really the case?
Edit the .prj file to include relative paths instead of absolute paths. This only works partially because the .prj file contains a section called MATLABPath which is always replaced with the current setpath of matlab. anyone which uses this file will have to check it out since it is being changed when used.
Find a way to generate the .prj file. the mcc documentation say: Project files created using either mcc or deploytool are eligible to use this option. suggesting there is a way to create a .prj file using mcc but I wasn't able to find how this can be done.
Is there a solution to this situation?
We ran into the same thing with Matlab Builder JA. Not only are the paths absolute, Matlab also adds other toolboxes that it finds on the path, which is irritating, as the build machine doesn't have licenses for a lot of them.
So what we do is:
Edit the prj file by hand to get rid of the absolute paths
Check it into source control and build with mcc
NEVER EVER check in the prj file after it has been touched by deploytool (do all changes by hand in an editor)
Our revision control lets you modify files without an explicit checkout, so using deploytool is not a problem. But why would you want to?
Not sure what the final packager does, but if it just bundles the MCR with the compiled binary, I would just write a replacement.
I personally use Visual Studio 2005 project to maintain my deployment projects and then convert the VCPROJ to PRJ on the fly using build command step
http://younsi.blogspot.com/2011/11/matlab-relative-path-issue-in-prj.html
Here's the mcc option documentation.
What I've found most useful is creating a standalone exe using mcc:
mcc -C -m <function.m> -a <fig> -a <dll> -a <etc> -d <outputPath>
The -C option tells mcc to generate the ctf file (which is the archive of all the compiled MATLAB stuff) as a separate file. I've had trouble on some target computers using a single exe with everything compiled in.
The -m option tells mcc to make an exe.
The -a options tell mcc to include the specified file in the package. For instance, if your script uses two fig files and a data file, you need a -a for each to make sure they get put in the output package.
To tell mcc to use a project file (I've never done this myself):
mcc -F <projectfile>
Also, if you're using R2009a on Windows, there's a known bug that requires some manifest manipulation.