使用Octave构建Mex文件(包装器问题)

我目前正在将一些代码从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文件的知识几乎不存在,我正在努力想办法摆脱这一堆错误 . 我甚至不知道我是否朝着正确的方向前进......欢迎任何帮助!

Edit 1:

我修改了我的wrappers.hpp文件,添加了 #include <stdlib.h> . 现在创建了一个mex文件 . 但是,当运行调用该文件的函数时,我现在收到以下错误:

error: failed to install .mex file function 'imResampleMex'
error: called from
    imResample at line 50 column 4

回答(1)

2 years ago

以下是我用来解决从Piotr的计算机视觉Matlab工具箱中为Octave创建mex文件的问题的步骤(仅涉及文件夹 channels 的cpp文件) .

工具箱附带的原始mex文件是为Matlab构建的,不适用于Octave . 从Octave构建它们时,会调用文件 wrappers.hpp . 必须通过在文件开头添加这两行来修改此文件: #include <stdlib.h>#include "mex.h" .

要构建mex文件,请在Octave提示符类型中(当位于cpp文件的目录中时):

mkoctfile --mex -DMATLAB_MEX_FILE the_file.cpp

这样就创建了Octave兼容的mex文件 .

编辑23/05/2017 - 在收到生成这些文件时出现问题的人的更多问题后,我在Github上发布了生成的文件:https://github.com/Eskapp/Piotr_vision_extrafiles_Octave . 随意使用它们 . 您需要手动将它们添加到Computer Vision工具箱中 .