mex 文件编译没有错误但在 matlab 中不起作用 [英] mex file compiled without errors but not working in matlab

查看:34
本文介绍了mex 文件编译没有错误但在 matlab 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我想编译 MatConvNet 库,以便在本教程的 windows 中使用(在 Windows 上编译 MatConvNet)但我不能.那我觉得还是先编译一个很简单的文件再编译库比较好.

我有 Matlab R2013a 64 位Visual Studio 2010 64 位.

我的程序Test.cpp

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

我可以使用 mex Test.cpp 在 matlab 中编译 Test.cpp当我输入 test 时,输出是 Hello!:)

我也可以根据下面的教程设置正确的配置并编译没有错误.

1) http://coachk.cs.ucf.edu/GPGPU/Compiling_a_MEX_file_with_Visual_Studio2.htm

2) http://www.orangeowlsolutions.com/archives/490

但是当我在 Matlab 中运行它时,没有任何反应.没有输出,Matlab 没有给我任何错误.

有什么问题?

请注意:

  1. 在(1)中的第二步是将matlabexterninclude"中的mexversion.rc"添加到项目中,但是我的电脑中没有这个文件,所以我做不到.

  2. 在 Visual Studio 中,我需要在下面添加两个标题以编译程序.

    • 包括stdafx.h"

    • 包含maxrix.h"

所以 Visual Studio 中的 Test.cpp 是:

#include "mex.h"#include "stdafx.h"#include "matrix.h"void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[]){printf("你好!:)
");}

解决方案

预编译的头文件

Visual Studio 版本的代码的一个问题是预编译的头文件 stdafx.h 导致编译器忽略它上面的任何代码(包括 mex.h):

#include "mex.h"#include "stdafx.h"//上面的任何内容都被忽略了!#include "matrix.h"

将 stdafx.h 包含移动到顶部或在项目设置中关闭 PCH 并删除包含.

<小时>

printfmexPrintf

在进入 MEX 项目设置之前,请注意 printf 指向 mexPrintfmex.h 提供:

#define printf mexPrintf

因此,使用 printf 不是问题,但可能不是好的做法.如果您在包含 mex.h 之后重新定义 printf 或由于 PCH 标头而无法获得此定义,则会出现问题.

<小时>

关于 Visual Studio 中的 MEX

我发布了一份更正式的设置 Visual Studio 项目以构建 MEX 文件的指南,作为对更多关于这个主题的常见参考问题,我还建议在这里使用 Visual Studio 属性表来设置您的项目以构建 MEX 文件.详细信息在引用的帖子中,但您只需要:

  1. 设置 MATLAB_ROOT 环境变量.
  2. 创建一个新的 DLL 项目.
  3. 在属性管理器下(从视图菜单),右键单击每个项目的构建配置和添加现有属性表...",选择 来自此 GitHub 存储库的 MATLABx64.props 文件.

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

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

解决方案

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.

这篇关于mex 文件编译没有错误但在 matlab 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆