Integrate MATLAB code in Visual Studio 2010 (C++) - c++

I've seen that it is possible to call MATLAB functions from VS2010 (VC++). I was wondering if it is possible to create executable files using VC++ that do not require any MATLAB software on the computer (like the MATLAB Compiler Runtime (MCR))?
I would like to create a program written in VC++ that calls some of the MATLAB functions, but is completely standalone and can run from any Windows computer without the need of any prior MATLAB software to have been installed.
Thanks.

It is not possible to install without MCR in any documented way and I even think that it is not legal.
However, there are some examples like Imatest, who managed to do it in some of their earlier versions.
Edit(1)
I almost forgot about MatlabCoder. If you buy it, it can transform your Matlab code directly into c code. But it will not work with some of the toolboxes, like image processing.

Related

Is it possible to get Visual Studio 2017 to compile c++ code into a file type other than exe? [duplicate]

This question already has answers here:
Can I program for Macs without owning one?
(3 answers)
Closed 3 years ago.
Recently I have been working on creating a game using Visual C++ 2017 and SDL2. I want to make my game accessible to users on all different kinds of systems, not just Windows. I tried starting with making my game also usable on Mac OS, but I cannot figure out any ways of doing this. I figured the best method would be to get Visual Studio to compile my code into a file type like ".APP" rather than ".EXE" since I don't own any form of Mac or Apple computer that can use software made to create apps for Mac. However, I cannot figure out how to change the options in Visual Studio to compile the code into a different file type or even if doing so is possible.
I've searched all over the internet for methods of converting ".EXE" to other file types, using different C++ compilers for compiling into Mac-compatible files, etc. but I haven't been able to find anything that works without requiring a virtual machine or having a physical Mac computer (neither of which I've tried yet since it would take a lot of effort and I'd like an easier solution).
If anybody could tell me how to change the type of file that Visual Studio compiles your code into or if they could suggest a different compiler that works on Windows, is compatible with SDL2, and can compile C++ code into file types that work on Mac computers like ".APP", that would solve my problem. Thank you in advance!
You need to compile your code on all the different platforms you want to support. Using a compiler supported on each of those platforms.
Code compiled on one platform can not run on another (except cross compiling, see below, but that's painful).
Cross compiling is an option on some platforms and with some compilers, but it's usually more pain than gain if you can in any way set up a local tool chain. Cross compilers generate code for a different platform than you are currently running, but getting that working can be hard. And you then also have no way to test your code on the target platform (unless you have one, in which case native compiling would just be easier in the first place).
You should automate the building of your code on different systems with different compilers in your Continuous Integration systen (CI) btw. So you always know after you push a commit, on what platforms it fails and why.
Easiest way forward (IMHO): Buy a cheap Mac. Buy a cheap PC and install Linux on it. Setup a CI system to build all code you commit to your code repository on all your systems (Windows, Mac, Linux) and make it scream at you when the build fails. Also use that system to build installable packages for each platform and again, scream when that fails.
Do the same for all your unit tests.
That's the only serious way to go forward supporting multiple platforms.

Unable to use dll built with Rcpp package in Visual Studio [duplicate]

I am wondering if there was a workaround for using the R package Rcpp within a Visual C++ project within Visual Studio. According to the Rcpp FAQ, this is not possible. I also came across another R package called R.NET. Can this only work with C# but there seems to be no examples from within Visual C++? Is there another option for this that I am missing? Even if I am forced to work with C#, can anyone envision latency issues with this for a high frequency trading environment?
Thanks for any feedback
We address this very question clearly in the Rcpp FAQ (which comes as vignette in the package, is on the CRAN site and its mirrors, and on my website).
In short, Rcpp supports the exact same toolchain which R supports. And on Windows that is MinGW (the port of gcc to Windows) provided by the Rtools package for R.
R Core only support MinGW, and we do the same. If one could compile R with Visual Studio, things would be easier, but as Rcpp sits on top of R we are playing the hand we are dealt with.
So sorry, the answer is a "No" to Visual Studio.
The question is what you really want to connnect with what. If you want to use R within C++ Rcpp seems to be the wrong decision as it connects C++ to R (and not the other way round as your question implies.).
If you want to connect R to C++ and use Visual C++ for creating your C++ code only (instead of linking R to C++), you might want to check this recent blog post (found via R-bloggers.com). It tells you a possible way of doing so.
Visual Studio works just fine with Rcpp. Has done for years. I disagree with Dirk Eddelbuettel.
This is an important fact - For example, there are very important libraries (Intels MKL, QT graphics framework) which are actually not compatible with minGW.
I maintain a (patched and cut-down) fork of Rcpp on github (https://github.com/rohan-shah/Rcpp) suitable for compilation with Visual Studio with cmake. The patch is something like five lines, I believe.
Disclaimer: It's fiddly to use, and you have to know what you're doing. If you do know what you're doing though, I've done quite a bit of the work for you. See https://github.com/rohan-shah/mpMap2 for an example of how to use patched Rcpp with Visual Studio.

Pseudocode Translation

I started working just a few hours ago on a pseudocode translator, which will translate a specific pseudocode, reguarding work with stacks and queues, to c/c++ executable code. The translator has education propuses.
I am still designing the project.
I've started thinking about how this could be done; I figured that maybe the best way to do this is to, in the first place, analize the pseudocode, change it to make it c/c++ code and then compiling it to make it an exe.
NOW, this would mean that the client machine SHOULD HAVE a c/c++ compiler installed on it.
As I'm working with .NET clases ( System.Collections.Generic.Queue(Of T) and System.Collections.Generic.Stack(Of T) ), I thought that a solution to this would be to use the same compiler visual studio uses to compile c/c++ code.
I've been investigating about this proccess, and as far as I know, the only way to compile c/c++ code by using a visual studio tool, is executing cl.exe from the Visual Studio Command Prompt. I found that information here at MSDN (article about how to manually compiling a c/c++ program)
So my first question is: does the USER versiĆ³n of .NET Framework (this means, assuming the user DOES NOT have Visual Studio) include a c/c++ compiler? If yes, is it the same included in visual studio, cl.exe? How can I get access to it? If no, is there a free compiler WITHOUT IDE I can include on my translator setup?
Notice that here we're talking about transforming a pseudocode string to executable c/c++ code here, the output string MUST BE COMPILED FROM THE USER PC, that's what the project is about.
There is no compiler included in the .Net framework - it is for running binary files produced by the compiler.
It's possible to get the compiler for free through for example the Visual Studio Express free download scheme. There are some license limitations on that ("no commercial use", if I remember correctly - so that would mean that if one of your customers uses your product, they can no sell the resulting code they have produced [and still be within the contract with MS]). Unfortunately, this isn't "without IDE", but the compiler that is part of the download is usable without the IDE, so if "without IDE" is simply that you want to be able to run the compiler with your program, then it will work.
If you really need a package that contains the compiler (in .Net variant) but no IDE code, then you will have to find a different solution - and for .Net I'm not sure there is one. The DDK (Device Driver Kit) contains the native C and C++ compilers. But it won't, as far as I understand, produce .Net code [I could be wrong].
Edit: It seems like if you are writing your own code, you can compile it using CSharpCodeProvider - however, this is not the same as a command-line compiler of cl.exe.
The long, the short, and the ugly is "no". The only C++ compiler remotely suitable for inclusion as a library is Clang, it's free and not toooooo bad to work with, and you'll have to write your own .NET binding, and don't forget to have fun with providing your own linker and stdlib.

Converting Matlab to C++

I need to convert some MATLAB code to C++. I am using Visual Studio 2010 and have MATLAB compiler installed. I am wondering how do I go about doing this.
Also when I publish my project will the end user have to install anything by MATLAB? (this cannot be the case)
You can compile Matlab scripts into standalone executables, but I'm not familiar with a Matlab tool to convert Matlab code to Visual C++ code. You can convert Matlab code to C or C++ using Simulink Coder (formerly Real-Time Workshop), which is available from Mathworks as a separate product.
To run the standalone executable, the user will have to install the Matlab Compiler Runtime. From the Matlab Compiler web page:
Executables and libraries created with the MATLAB Compiler product use a runtime engine called the MATLAB Compiler Runtime (MCR). The MCR is provided with MATLAB Compiler for distribution with your application and can be deployed royalty-free.
You will not get rid of the Matlab runtimes. I often had problems using Matlab Compiler. It basically is like a wrapper around the Matlab runtime, generates clumsy code and did introduce many problems for deployment. C alternatives ... ? I could not find any. So I recently switched to ilnumerics, which is like Matlab in C# - only much faster and no translation necessary. it works out great so far because we use ASP.NET anyway and hence save jumping between languages and can directly use the 3d plots of ilnumerics.
Another option you have is to use the MATLAB Engine. It probably isn't an option for real-time applications, but otherwise it's really nice. You basically call MATLAB functions right from C++. Figures (plots) will be display just as if you were running local MATLAB. When the program starts, a MATLAB console is created.

OpenCV, Matlab and STL containers

Many functions in the latest release of OpenCV require the use of STL containers. I run into problems when trying to use them in a Matlab MEX file. I am compiling the MEX files from within Matlab. Both OpenCV and Matlab use the "/MD" flag which is "Multithreaded DLL" for code generation.
Compiler: MSVC++ 9.0
Matlab 2010a
OpenCV latest from SVN, 2.11 I think.
The code I am using is very simple:
vector<KeyPoint> keypoints_vec;
SurfFeatureDetector surf;
surf.detect(cvImg,keypoints_vec);
This compiles but crashes when run in a Matlab MEX file. The crash is within OpenCV in vector::resize. The old interface (without STL containers) works fine but is deprecated. How can I use STL containers between Matlab and OpenCV?
I fought with this very problem in the last two days. The problem is this:
libmex.dll (and a whole Matlab) uses Microsoft.VC80.CRT (version=8.0.50727.4053)
But your OpenCV uses Microsoft.VC90.CRT (version=9.0.21022.8)
So you can use the previous version of VC (VS 2005 with SP1 as far as I know), or as a workaround, you can use gcc (MINGW) (in this case they use totally different stl, so they can't interfere).
I did the latter and it works, and it will work with the next versions of Matlab (hopefully).
A long time ago I had problems with Matlab <-> VS interop. It might be some microsoft visual c++ runtime library discrepancy. Check what runtime lib is required by matlab and what version does your visual studio have. I remember using Depends to get the dll dependencies for my program. Check your call stack after crashing (by attaching your msdev debugger) it might give you some hints.
It was a long time ago so I'm just giving hints of what I remember.
I had a similar problem in the past few days, and was able to resolve the issue with some help from the friendly folks at MathWorks.
From the original post at http://www.mathworks.com/matlabcentral/answers/9294-mex-dynamic-memory-management-issue-with-std-vector-in-linked-external-dll-segmentation-error :
You are probably seeing an incompatibility between the stl library and or compiler options used by your pre-compiled dll and those used by MATLAB and the MEX command. MATLAB 2009b was built with MSVC 2005.
You may be able to fix the problem by changing the options used by mex or by building your mex file directly with MSVC. One example of an option that may effect things is SECURE_SCL=0. I would start by building your test program with the options MATLAB is using to find the problematic option then try removing that option when building the mex file.
Because of this sort of incompatibility use of stl objects in the api's of third party compiled libraries is usually a bad idea.
Following his advice, I removed the SECURE_SCL=0 option from the mex options file at
C:\Users\ThePit\AppData\Roaming\MathWorks\MATLAB\R2009b\mexopts.bat
Then recompiled the mex file, now everything works like a charm - the function is returning the correct data and segmentation error no longer occurs.
The data in a vector should still be stored as a single contiguous block
std::vector<int> data;
int *array = &data[0];
int *array = &data.front();
Should give you 'c' style pointers to the data, try passing these to matlab
see also: How does the C++ STL vector template store its objects in the Visual Studio compiler implementation?