mex file compiled without errors but not working i

2019-07-05 20:06发布

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");
}

3条回答
smile是对你的礼貌
2楼-- · 2019-07-05 20:33

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.
查看更多
贼婆χ
3楼-- · 2019-07-05 20:44

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!

查看更多
疯言疯语
4楼-- · 2019-07-05 20:45

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.
查看更多
登录 后发表回答