【问题标题】:Mex file building with Octave (issue with wrappers)使用 Octave 构建 Mex 文件(包装器问题)
【发布时间】: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 &lt;stdlib.h&gt;。现在创建了一个 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 &lt;stdlib.h&gt;。这应该可以解决 size_tfree 错误,但不确定是否能解决所有问题。
  • @Praetorian 非常感谢您的意见!有了这个添加,现在创建了一个 mex 文件。但是,在运行函数error: failed to install .mex file function 'imResampleMex' 时会出现一个新错误。我现在正在编辑我的问题。
  • 我对 Octave 的墨西哥风味不是很熟悉,所以我可能无法提供帮助。 Google 搜索表明您的 mex 文件有问题,例如,mexFunction 未正确导出?你知道MATLAB_MEX_FILE 是否被定义了吗?确保它是,所以 mex.h 被包括在内。一旦你定义了它,你可能会发现你甚至不需要#include &lt;stdlib.h&gt;
  • wrappers.h的开头,你会看到#ifdef根据MATLAB_MEX_FILE是否被定义来切换。 mex 自动定义了这个符号,我不知道mkoctfile 是否定义。您应该尝试恢复您所做的所有更改并尝试mkoctfile --mex -DMATLAB_MEX_FILE imResampleMex.cpp,这可能是您让事情正常工作所需要的。
  • 好吧,写一个答案来解释你所学到的东西,以便将来对其他人有所帮助:)

标签: c++ octave wrapper mex


【解决方案1】:

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

工具箱附带的原始 mex 文件是为 Matlab 构建的,不适用于 Octave。在从 Octave 构建它们时,会调用文件 wrappers.hpp。 必须通过在文件开头添加这两行来修改此文件:#include &lt;stdlib.h&gt;#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。随意使用它们。 您需要手动将它们添加到计算机视觉工具箱中。

【讨论】:

  • 嗨@Eskapp 我现在正在做和你一样的事情。 Matlab 到 Octave 与 mdp_tracker。毕竟这个mex文件问题。还有一些matlab依赖问题,比如median.m中断。我只是好奇你是否有这些 octave 代码的开源版本?
  • 嘿@Pwan,我想我可以将它们推送到 GitHub 上。要试试。给我一两天我找回这些文件;)
  • HI Eskapp!! 非常感谢!因为我也转换了它并运行了演示。但我认为我的代码中可能仍然存在一些错误。您的代码将有很大帮助。您成功运行演示了吗?
  • 我只运行了我的代码工作所需的函数。我明天将尝试检索文件。留在原地;)
  • @Pwan github.com/Eskapp/Piotr_vision_extrafiles_Octave 您需要手动将文件添加到主工具箱。让我知道它是否有效。如果没有,请发布错误消息:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-20
相关资源
最近更新 更多