Mex file compiled without error but doesn't work in matlab

At first I wanted to compile the MatConvNet library for use as the window of this tutorial ( Compiling MatConvNet on Windows ) but I couldn't. Then I think it's better to compile a very simple file and then compile the library after that.

I have Matlab R2013a 64 bit and Visual Studio 2010 64 bit .

my Test.cpp program

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

      

I can compile Test.cpp in matlab using mex Test.cpp And when I type test , the output is Hello! :)

I can also set the correct configuration according to the tutorials below and compile without error.

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 happens. There is no way out and Matlab doesn't give me any errors.

What is the problem?

Note:

  • in (1) the second step adds "mexversion.rc" from "matlab \ extern \ include" to the project. But this dose of the file doesn't exist on my computer, so I couldn't do it.

  • In Visual Studio, I needed to add the header headers below to compile the program.

    • include "stdafx.h"

    • enable "maxrix.h"

so Test.cpp in Visual Studio:

#include "mex.h"
#include "stdafx.h"
#include "matrix.h"

void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
    printf("Hello! :)\n");
}

      

+3


source to share


3 answers


Shenanigans precompiled header

The problem with the Visual Studio Code version is the precompiled stdafx.h header file causing the compiler to ignore any code above it (including mex.h):

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

      

Move stdafx.h to the top or turn off PCH in project settings and remove include.


printf

against mexPrintf

Before entering the MEX project, please note what printf

the mexPrintf

courtesy indicates mex.h

:



#define printf mexPrintf

      

So usage is printf

not a problem, but probably not good practice. The problem arises if you override printf

mex.h after including, or don't get this definition because of the PCH header.


Regarding MEX in Visual Studio

I've posted a more formal guide on setting up Visual Studio projects to generate MEX files as an answer to a more frequently asked question on this topic, and I'll also suggest using the Visual Studio property sheets here to get your project set up to generate a MEX file. Details are in the link, but you just need:

  • Set the environment variable MATLAB_ROOT

    .
  • Create a new DLL project.
  • In the Properties Manager section (from the View menu), right-click on each project's build configuration and Add Existing Property Sheet ... while choosing MATLABx64. file props from this GitHub repository .
+3


source


printf

only works on native C. You need to use mexPrintf

. Therefore, your code should be as follows:

#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 does not appear on the MATLAB command line. If you want to display messages in MATLAB, you need to use mexPrintf

instead printf

.

To be clear, if you look at the documentation mexPrintf

, you can see a caveat at the end:

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




BTW I recommend this awesome MEX tutorial here: http://classes.soe.ucsc.edu/ee264/Fall11/cmex.pdf . This is the tutorial I used to get started with MEX programs in MATLAB. You will also see that the first example is of the same Hello World caliber that you are trying to run :)


Good luck!

+1


source


How to compile a C / CPP file and create a mex file for use in Matlab

I found a solution using @rayryeng help and ( How do I create a mex file directly in Visual Studio? ) @Jorre 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 the mex file. You can change it later.

2. Change the debug . Release .

Right click on project -> Properties -> Configuration Properties -> General

  1. Install target extension to .mexw64

  2. Set the configuration type to Dynamic Link Library (DLL)

Configuration Directions -> VC ++ Directories:

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

Configuration Properties -> Linker -> General:

  1. Add $ (MATLAB_ROOT) \ extern \ lib \ win64 \ microsoft; 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 have to install the platform to x64, otherwise you will get an error like "Error 1 error LNK2001: unresolved external symbol _mexPrintf".

9. Configuration Properties -> Configuration Manager -> Active Solution Platform -> New -> x64 -> Copy Options from Win32

Now you can compile your file and get the mex file.

If you see these tutorials, there are other things that are unnecessary and may cause problems. ( http://coachk.cs.ucf.edu/GPGPU/Compiling_a_MEX_file_with_Visual_Studio2.htm )

  • Create an empty project, not an MFC Dll project.
  • No need for a * .def file.
  • You don't need to add "mexversion.rc" to your project.
  • There is no need to add "MATLAB_MEX_FILE" as a preprocessor definition.
0


source







All Articles