First I wanted to compile MatConvNet library for using in windows form this tutorial (Compiling MatConvNet on Windows) but I couldn't. Then I think it is better to compile a very simple file and after that going to compile the library.
I have Matlab R2013a 64 bit and Visual Studio 2010 64 bit.
my program Test.cpp
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
printf("Hello! :)\n");
}
I can compile Test.cpp in matlab with mex Test.cpp And when I type test output is Hello! :)
I also can set the correct configuration according to tutorials below and compile it without errors.
1) http://coachk.cs.ucf.edu/GPGPU/Compiling_a_MEX_file_with_Visual_Studio2.htm
2) http://www.orangeowlsolutions.com/archives/490
But when I run it in Matlab, nothing happen. There is no output and Matlab doesn't give me any error.
What is the problem?
Notice that:
in (1) second step is adding "mexversion.rc" from "matlab\extern\include" to the project But this file dose not exist in my computer so I couldn't do it.
In Visual Studio I needed to add tow headers in below for compiling the program.
include "stdafx.h"
include "maxrix.h"
so the Test.cpp in Visual Studio is:
#include "mex.h"
#include "stdafx.h"
#include "matrix.h"
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
printf("Hello! :)\n");
}
How To Compile a C/CPP File and Create mex File for Using in Matlab
I found the solution by @rayryeng help and (How to build mex file directly in Visual Studio?) @jorre's post.
This is tested with Matlab R2013a 64 bit and Visual Studio 2010 64 bit.
Test.cpp
1.Create a new project in VS -> Visual C++ -> General -> Empty Project. The name of your Project will be the name of mex file. You can change it later.
2.Change the Debug to Release.
Right click on the project -> Properties -> Configuration properties -> General
Set Target Extension to .mexw64
Set Configuration Type to Dynamic Library (.dll)
Configureation poperties -> VC++ Directories:
5.Add $(MATLAB_ROOT)\extern\include; to Include Directories
Configuration properties -> Linker -> General:
Configuration properties -> Linker -> Input:
libmx.lib
libmex.lib
libmat.lib
Configuration properties -> Linker -> Command Line:
Now you must set you platform to x64 otherwise you'll get error like"Error 1 error LNK2001: unresolved external symbol _mexPrintf".
9.Configuration Properties -> Configuration Manager -> Active Solution Platform -> New -> x64 -> Copy Settings From Win32
Now you can compile your file and get mex file.
If you see these tutorials there are other things thar are not needed and may cause problems. (http://coachk.cs.ucf.edu/GPGPU/Compiling_a_MEX_file_with_Visual_Studio2.htm)
printf
only works in native C. You need to usemexPrintf
. Therefore, your code should be this:In general, printing to standard output in a MEX script doesn't appear in the MATLAB command prompt. If you want to display messages in MATLAB, you need to use
mexPrintf
instead ofprintf
.To be explicit, if you consult the
mexPrintf
documentation, a caveat can be seen towards the end:BTW, I recommend this awesome MEX tutorial here: http://classes.soe.ucsc.edu/ee264/Fall11/cmex.pdf. This is the tutorial that I used to get started with programming MEX wrappers in MATLAB. You'll also see that the first example is of the same "Hello World" caliber that you are trying to get running :)
Good luck!
Pre-compiled header shenanigans
A problem with the Visual Studio version of the code is the pre-compiled header file stdafx.h causing the compiler to ignore any code above it (the mex.h include):
Move the stdafx.h include to the top or turn off PCH in projects settings and remove the include.
printf
vs.mexPrintf
Before getting into MEX project setup, note that
printf
points tomexPrintf
courtesy ofmex.h
:So, using
printf
is not an issue, but probably not good practice. The problem comes if you redefineprintf
after including mex.h or fail to get this define on account of the PCH header.Regarding MEX in Visual Studio
I posted a more formal guide to setting up a Visual Studio projects for building MEX files as an answer to the more commonly-reference question on this topic, and I would also suggest here to use Visual Studio property sheets to get your project set up to build a MEX file. The details are in the referenced post, but you just need to:
MATLAB_ROOT
environment variable.