【发布时间】:2017-04-15 05:40:34
【问题描述】:
我目前正在将一些代码从 Matlab 移植到 Octave。 Matlab 代码的一些功能使用了 Piotr 的计算机视觉 Matlab 工具箱 (here),其中包含一些 mex 文件。 在 Matlab 中,一切都像一个魅力,但是当我用 Octave 运行我的代码时,它会抛出这个错误:
error: 'imResampleMex' undefined near line 50 column 5
但是工具箱中的所有内部路径都应该是好的。我发现 Matlab 和 Octave 处理 mex 文件的方式不同,并尝试从 Octave 中的 C++ 函数构建一个 mex 文件,如下所示:
mkoctfile --mex imResampleMex.cpp
它失败并抛出以下与 C++ 包装函数相关的错误消息:
In file included from imResampleMex.cpp:6:0:
wrappers.hpp:21:24: error: 'wrCalloc' declared as an 'inline' variable
inline void* wrCalloc( size_t num, size_t size ) { return calloc(num,size);
^
wrappers.hpp:21:24: error: 'size_t' was not declared in this scope
wrappers.hpp:21:36: error: 'size_t' was not declared in this scope
inline void* wrCalloc( size_t num, size_t size ) { return calloc(num,size);
^
wrappers.hpp:21:48: error: expression list treated as compound expression in initializer [-fpermissive]
inline void* wrCalloc( size_t num, size_t size ) { return calloc(num,size);
^
wrappers.hpp:21:50: error: expected ',' or ';' before '{' token
inline void* wrCalloc( size_t num, size_t size ) { return calloc(num,size);
^
wrappers.hpp:22:24: error: 'wrMalloc' declared as an 'inline' variable
inline void* wrMalloc( size_t size ) { return malloc(size); }
^
wrappers.hpp:22:24: error: 'size_t' was not declared in this scope
wrappers.hpp:22:38: error: expected ',' or ';' before '{' token
inline void* wrMalloc( size_t size ) { return malloc(size); }
^
wrappers.hpp: In function 'void wrFree(void*)':
wrappers.hpp:23:44: error: 'free' was not declared in this scope
inline void wrFree( void * ptr ) { free(ptr); }
^
wrappers.hpp: At global scope:
wrappers.hpp:28:17: error: 'size_t' was not declared in this scope
void* alMalloc( size_t size, int alignment ) {
^
wrappers.hpp:28:30: error: expected primary-expression before 'int'
void* alMalloc( size_t size, int alignment ) {
^
wrappers.hpp:28:44: error: expression list treated as compound expression in initializer [-fpermissive]
void* alMalloc( size_t size, int alignment ) {
^
wrappers.hpp:28:46: error: expected ',' or ';' before '{' token
void* alMalloc( size_t size, int alignment ) {
^
imResampleMex.cpp: In function 'void resampleCoef(int, int, int&, int*&, int*&, T*&, int*, int)':
imResampleMex.cpp:21:39: error: 'alMalloc' cannot be used as a function
wts = (T*)alMalloc(nMax*sizeof(T),16);
^
imResampleMex.cpp:22:43: error: 'alMalloc' cannot be used as a function
yas = (int*)alMalloc(nMax*sizeof(int),16);
^
imResampleMex.cpp:23:43: error: 'alMalloc' cannot be used as a function
ybs = (int*)alMalloc(nMax*sizeof(int),16);
^
imResampleMex.cpp: In function 'void resample(T*, T*, int, int, int, int, int, T)':
imResampleMex.cpp:48:43: error: 'alMalloc' cannot be used as a function
T *C = (T*) alMalloc((ha+4)*sizeof(T),16); for(y=ha; y<ha+4; y++) C[y]=0;
^
warning: mkoctfile: building exited with failure status
wrappers.hpp 文件如下所示:
#ifndef _WRAPPERS_HPP_
#define _WRAPPERS_HPP_
#ifdef MATLAB_MEX_FILE
// wrapper functions if compiling from Matlab
#include "mex.h"
inline void wrError(const char *errormsg) { mexErrMsgTxt(errormsg); }
inline void* wrCalloc( size_t num, size_t size ) { return mxCalloc(num,size); }
inline void* wrMalloc( size_t size ) { return mxMalloc(size); }
inline void wrFree( void * ptr ) { mxFree(ptr); }
#else
// wrapper functions if compiling from C/C++
inline void wrError(const char *errormsg) { throw errormsg; }
inline void* wrCalloc( size_t num, size_t size ) { return calloc(num,size); }
inline void* wrMalloc( size_t size ) { return malloc(size); }
inline void wrFree( void * ptr ) { free(ptr); }
#endif
// platform independent aligned memory allocation (see also alFree)
void* alMalloc( size_t size, int alignment ) {
const size_t pSize = sizeof(void*), a = alignment-1;
void *raw = wrMalloc(size + a + pSize);
void *aligned = (void*) (((size_t) raw + pSize + a) & ~a);
*(void**) ((size_t) aligned-pSize) = raw;
return aligned;
}
// platform independent alignned memory de-allocation (see also alMalloc)
void alFree(void* aligned) {
void* raw = *(void**)((char*)aligned-sizeof(void*));
wrFree(raw);
}
#endif
我想我需要修改这个文件,但我对 C++ 和 mex 文件的了解几乎不存在,我正在努力寻找摆脱这堆错误的方法。我什至不知道我是否朝着正确的方向前进......欢迎任何帮助!
编辑 1:
我修改了我的 wrappers.hpp 文件,在其中添加了 #include <stdlib.h>。现在创建了一个 mex 文件。但是,在运行调用文件的函数时,我现在收到以下错误:
error: failed to install .mex file function 'imResampleMex'
error: called from
imResample at line 50 column 4
【问题讨论】:
-
也许 Matlab 的
mex.h版本引入了一些 Octave 版本没有的 C 标准库头文件。尝试在wrappers.hpp中添加#include <stdlib.h>。这应该可以解决size_t和free错误,但不确定是否能解决所有问题。 -
@Praetorian 非常感谢您的意见!有了这个添加,现在创建了一个 mex 文件。但是,在运行函数
error: failed to install .mex file function 'imResampleMex'时会出现一个新错误。我现在正在编辑我的问题。 -
我对 Octave 的墨西哥风味不是很熟悉,所以我可能无法提供帮助。 Google 搜索表明您的 mex 文件有问题,例如,
mexFunction未正确导出?你知道MATLAB_MEX_FILE是否被定义了吗?确保它是,所以mex.h被包括在内。一旦你定义了它,你可能会发现你甚至不需要#include <stdlib.h> -
看
wrappers.h的开头,你会看到#ifdef根据MATLAB_MEX_FILE是否被定义来切换。mex自动定义了这个符号,我不知道mkoctfile是否定义。您应该尝试恢复您所做的所有更改并尝试mkoctfile --mex -DMATLAB_MEX_FILE imResampleMex.cpp,这可能是您让事情正常工作所需要的。 -
好吧,写一个答案来解释你所学到的东西,以便将来对其他人有所帮助:)