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

标签 c++ octave wrapper 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 文件的了解几乎不存在,我正在努力寻找摆脱这堆错误的方法。我什至不知道我是否正朝着正确的方向前进......欢迎任何帮助!

编辑 1:

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

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

最佳答案

以下是我用来解决从 Piotr 的计算机视觉 Matlab 工具箱创建 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 .随意使用它们。 您需要将它们手动添加到计算机视觉工具箱。

关于c++ - 使用 Octave 构建 Mex 文件(包装器问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40896162/

相关文章:

CoffeeScript 使用传递的参数创建包装函数

android - 应用程序中心:构建失败-找不到gradleWrapperMain

c++ - FSPathMakeRef 和 FSGetCatalogInfo 的 Cocoa 等价物是什么?

c++ - std::ifstream 缓冲区缓存

matlab - Octave 绘图点作为动画

algorithm - 在 GNU Octave 中计数条目等于零

java - Java 中的 Matlab/Octave 类型功能 - 在哪里查看

c++ - 围绕 C++ 类的 C 可调用包装器

具有内存错误访问的 C++ struct new

c++ - 如何使用 RapidXML C++ 检查 xml 中的空标记