Going back and forth between Matlab and other languages is a must if you need to save time using either your code written in other languages (in my case here C++) or using pre-built libraries.
I struggled a lot, trying to use loadlibrary command in Matlab to execute some commands from a dll, either Matlab doesn't find the function in the library, or with compatibility issues and so many other problems.
So I went on to use mex functions. It was going OK in Matlab till I needed to use libraries that are based on libraries that are based on other libraries, and the client has an older version of Matlab with an old system.
It became complex and debugging to know the error was a problem for an inexperienced programmer like me.
So I went to use the ability to compile mex files in Visual Studio, where the Environment is already setup for all the old libraries and I need only to do some project configurations, and it worked :).
I am writing this right now to first help myself document this to use in the future, and maybe someone else is having the same struggle as me, and this can help to find the way.
My setup used in this article is Matlab R2015b, VS2017, Win 7.
The first thing I did is to search for suitable configurations to build mex files in VS, I found many pages on the internet, 2 methods were the ones that really helped me:
Method 1: using the steps in How to build mex file directly in Visual Studio?
Method 2: using the steps in MEX Function Templates
Method 1: Setting the configurations manually
Knowing which configurations affect your project is good knowledge to have, this is why I am explaining this method first, if you want a faster solution go to Method 2.
Start an empty C++ project with the following settings in the project's Property Pages to build a working .mex function from Visual Studio, remember to repeat these for your different project build configurations (debug and release for example if needed):
Set Target Extension to .mexw32 (or .mexw64 depending on your environment)
Set Configuration Type to Dynamic Library (.dll)
2- Configureation poperties -> VC++ Directories:
Add $(MATLAB_ROOT)\extern\include; to Include Directories
Configuration properties -> Linker -> General:
Add $(MATLAB_ROOT)\extern\lib\win64\microsoft; to Additional Library Directories
Configuration properties -> Linker -> Input:
Add libmx.lib;libmex.lib;libmat.lib; to Additional Dependencies
Configuration properties -> Linker -> Command Line:
Add /export:mexFunction to Additional Options
$(MATLAB_ROOT) is the path to Matlab's root folder, eg. C:\Program Files\MATLAB\R2013a.
So far this has only been tried from a solution created from scratch and built for Matlab 2013a 64-bit. I assume that to build for 32-bit one only needs to change both occurrences of 64 to 32. I will update the post when I have confirmed that this works for an existing solution.
EDIT: As expected this works for projects added to existing solutions. Remember to set the new project to be dependent on the project that creates the library.
Edit 2: Following this question I can confirm that the above steps work in Visual Studio 2012, 2013, and 2017 too.
Links that may help:
Visual Studio Toolbar for mex interface with video tutorial
Compiling a MEX file with Visual Studio
How to build mex file directly in Visual Studio

No comments:
Post a Comment