【问题标题】:Matlab R2015b 64 bits Error using mexMatlab R2015b 64位错误使用mex
【发布时间】:2015-12-24 19:10:53
【问题描述】:

我想用 Matlab R2015b 编译以下代码"

#include "mex.h"
#include "GLTree.cpp"
/* the gateway function */
//la chiamata deve essere DeleteGLtree(Tree)

void mexFunction( int nlhs,const mxArray *plhs[], 
    int nrhs, const mxArray *prhs[1]) {


//dichiarazione variabili
GLTREE* Tree;
double *ptrtree;



 if(nrhs!=1){ mexErrMsgTxt("Only one input supported.");}




ptrtree = mxGetPr(prhs[0]);//puntatore all'albero precedentemente fornito


Tree=(GLTREE*)((long)(ptrtree[0]));//ritrasformo il puntatore passato

if(Tree==NULL)
{ mexErrMsgTxt("Invalid tree pointer");
}


//chiamo il distruttore

 delete Tree;  }

但我收到此错误“C:\Users\Admin\Documents\MATLAB\GraphSeg\GLtree3DMex\DeleteGLTree.cpp:15:38: 警告:从不同大小的整数转换为指针 [-Wint-to-pointer-投掷] 树=(GLTREE*)((long)(ptrtree[0]));"

【问题讨论】:

  • 那么问题是什么?
  • @EluciusFTW 如何使用 Matlab R2015b 64 位成功编译代码?

标签: c++ matlab mex


【解决方案1】:

您错误地声明了 mexFunction。您的声明:

void mexFunction( int nlhs,const mxArray *plhs[], int nrhs, const mxArray *prhs[1])

不等于:

void mexFunction( int nlhs,mxArray *plhs[], int nrhs, const mxArray *prhs[])

回答您的问题

您需要在mxArray *plhs[] 之前删除const

更多内容:

您可能想查看此链接,了解如何将内存地址从 mex 函数传回 MATLAB。我的直觉是,您随意使用 double 并强制转换为 long(甚至是 long long)可能会带来极大的问题......它确实应该是 uin64,并且为了稳健性,您可能需要一些额外的编译检查类型一切都匹配,一切都是 8 个字节...... http://www.mathworks.com/matlabcentral/answers/75524-returning-and-passing-void-pointers-to-mex-functions

【讨论】:

  • 是的,你是对的,非常感谢你的回答
【解决方案2】:

这是基于您的代码的猜测(我没有编译它):在 64 位机器上,地址空间的指针大小为 8 字节(64 位),并且您将指针转换为 long 类型可能只有 4 个字节长。如果要强制转换,则应使用 8 字节长类型,例如 long long(保证至少为 8 字节)

【讨论】:

  • 感谢您的回答,我使用“long long”强制转换编译了代码,但这次我得到以下错误“使用'MinGW64编译器(C++)'构建。使用mex时出错无法导出mexFunction:符号未定义 collect2.exe:错误:ld 返回 1 个退出状态"
  • 至少在带有 clang-700.1.81 的 Mac OS X 上,long 是 8 个字节。我想在现代 Windows 编译器上 long 也是 8 个字节。
  • @malmo:这是一个链接错误。也许你的 mexopts 文件有错误的定义。根据这个错误很难判断。
  • @MatthewGunn 在我的系统上(win 8.1 64 位上的 Visual Studio 2013)长为 4 字节。这实际上取决于实现,long 实际上只能保证其最小尺寸。
猜你喜欢
  • 1970-01-01
  • 2011-10-09
  • 1970-01-01
  • 1970-01-01
  • 2016-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多