编译没有错误,但不是工作在MATLAB MEX文件 [英] mex file compiled without errors but not working in matlab

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

问题描述

首先,我想编译MatConvNet库使用Windows窗体本教程 (编译MatConvNet在Windows ) 但我不能。然后,我想还是要去编译库后,编译一个很简单的文件。

我的 Matlab的R2013a 64位 Visual Studio 2010的64位

我的程序Test.cpp的

 的#includemex.h
无效mexFunction(INT nlhs,mxArray * plhs [],int的nrhs,常量mxArray * prhs [])
{
    的printf(你好!:)\ N);
}
 

我可以在MATLAB中进行编译Test.cpp的 MEX Test.cpp的 当我键入​​测试输出您好! :)

我也可以按照下面的教程设置正确的配置并没有错误编译。

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)第二步是从加入mexversion.rcMATLAB \的extern \包括到项目,但该文件剂量在我的电脑不存在的,所以我不能这样做。

  2. 在Visual Studio中,我需要在下面加拖头的编译程序。

    • 的#includestdafx.h中
    • 的#includemaxrix.h

所以 Test.cpp的在Visual Studio是:

 的#includemex.h
#包括stdafx.h中
#包括matrix.h

无效mexFunction(INT nlhs,mxArray * plhs [],int的nrhs,常量mxArray * prhs [])
{
    的printf(你好!:)\ N);
}
 

解决方案

pre-编译头有心计

与Visual Studio版本的code的一个问题是pre编译头文件stdafx.h中的从而使编译器忽略它上面的任何code (中mex.h包括):

 的#includemex.h
#包括stdafx.h中//任何高于这里被忽略!
#包括matrix.h
 

将stdafx.h中包括顶端或关闭PCH在项目设置和删除包含。


的printf mexPrintf

在进入墨西哥项目设置,注意的printf 指向 mexPrintf 礼貌的mex.h

 #定义printf的mexPrintf
 

所以,使用的printf 不是问题的,但可能不是好的做法。如果你重新定义的printf 包括mex.h后或无法得到这个定义是考虑到PCH头的问题就来了。


关于MEX在Visual Studio

我贴一个比较正式的指导,建立一个Visual Studio项目,以构建MEX文件作为一个答案更多通常参照有关这个主题的问题,我也将在这里建议使用Visual Studio的属性表,让您的项目落户建设MEX文件。细节是在参考职位,但你只需要:

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

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

解决方案

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.

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

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