3

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:

  1. 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.

  2. 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");
}
Community
  • 1
  • 1
Iman
  • 424
  • 5
  • 18

3 Answers3

3

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):

#include "mex.h"
#include "stdafx.h" // ANYTHING above here is IGNORED!
#include "matrix.h"

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 to mexPrintf courtesy of mex.h:

#define printf mexPrintf

So, using printf is not an issue, but probably not good practice. The problem comes if you redefine printf 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:

  1. Set the MATLAB_ROOT environment variable.
  2. Create a new DLL project.
  3. Under Property Manager (from View menu), right click on each project's build configuration and "Add Existing Property Sheet...", choosing the MATLABx64.props file from this GitHub repo.
Community
  • 1
  • 1
chappjc
  • 30,359
  • 6
  • 75
  • 132
1

printf only works in native C. You need to use mexPrintf. Therefore, your code should be this:

#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
    mexPrintf("Hello! :)\n");
}

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 of printf.

To be explicit, if you consult the mexPrintf documentation, a caveat can be seen towards the end:

In a C MEX-file, you must call mexPrintf instead of printf to display a string.


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!

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • @rayryeng I used mexPrintf and IntelliSense recongnized it but I got this error: Error 2 error C3861: 'mexPrintf': identifier not found – Iman Dec 09 '14 at 16:38
  • @Iman - Compile in it MATLAB. Don't compile it in VS. Also, the step where you need to include `mexversion.rc` is required if you want this to compile successfully in VS. The best way to compile your code is to do it directly in MATLAB. All of the dependencies are set up. – rayryeng Dec 09 '14 at 16:38
  • @rayryeng As I mentioned, I could compile this in Matlab! I want to compile it in VS. Because I want to compile MatConvNet in VS. – Iman Dec 09 '14 at 16:40
  • @Iman - Well in any case, `printf` does not show anything because MATLAB doesn't show that output in the command prompt. You need to use `mexPrintf`. Did you try any other methods from `mex.h` to see if you're getting that error? It's more likely you don't have the environment set up properly more than anything. However, if it isn't prudent that you print out debug statements, you could simply not use it if you want. – rayryeng Dec 09 '14 at 16:42
  • @rayryeng How can I get "mexversion.rc"? This file not exist in my computer!!! Even I Install Matlab R2011b but there was not exist in that version too. What can I do? – Iman Dec 09 '14 at 16:43
  • @Iman - Check this page: http://stackoverflow.com/questions/16716821/how-to-build-mex-file-directly-in-visual-studio-2010 . It seems those instructions you consulted are out of date. – rayryeng Dec 09 '14 at 16:47
  • Ok. I'll check it. But why printf works in Matlab? Because I could compiled it in Matlab. – Iman Dec 09 '14 at 16:49
  • And why Matlab doesn't give me any error When I run created mex file with VS that has printf? – Iman Dec 09 '14 at 16:52
  • @Iman - Because `mex.h` does include `` in the header file, so `printf` is available to you. However, using it is basically useless as it won't output to the command prompt. Just read the post and ask another question if you're stuck. I'll stop commenting until you're stuck again. – rayryeng Dec 09 '14 at 16:55
  • @rayryeng Thanks so much! I have been working on this all the day. Actually I saw this post, but I don't pay attention to it because I have read lots of thing and this post was very similar. but it has just one thing that the post did't mentioned well. I will explain the complete solution in next post for other people that encounter with this problem and search on the internet for solution. – Iman Dec 09 '14 at 17:44
  • @Iman - Sounds good! Please do. I'm curious to know how you solved it. – rayryeng Dec 09 '14 at 17:45
  • @Shai :))) Yes! That was the problem! :D – Iman Dec 09 '14 at 17:47
0

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

#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
    mexPrintf("Hello World123! :)\n");
}

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

  1. Set Target Extension to .mexw64

  2. Set Configuration Type to Dynamic Library (.dll)

Configureation poperties -> VC++ Directories:

5.Add $(MATLAB_ROOT)\extern\include; to Include Directories

Configuration properties -> Linker -> General:

  1. Add $(MATLAB_ROOT)\extern\lib\win64\microsoft; to Additional Library Directories

Configuration properties -> Linker -> Input:

  1. Add these things to Additional Dependencies

libmx.lib

libmex.lib

libmat.lib

Configuration properties -> Linker -> Command Line:

  1. Add /export:mexFunction to Additional Options

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)

  1. Create an empty Project, not a MFC Dll Project.
  2. There is no need to *.def file.
  3. There is no need to add "mexversion.rc" into your project.
  4. There is no need to add "MATLAB_MEX_FILE" as a preprocessor definition.
Community
  • 1
  • 1
Iman
  • 424
  • 5
  • 18
  • I use property sheets to automate these settings. See here: http://stackoverflow.com/a/26000174/2778484. I'll post an answer here with screenshots and sources on github. – chappjc Dec 09 '14 at 23:06