使用Octave构建Mex文件(带有包装的问题) [英] Mex file building with Octave (issue with wrappers)

查看:132
本文介绍了使用Octave构建Mex文件(带有包装的问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将一些代码从Matlab移植到Octave. Matlab代码的某些功能使用了Piotr的Computer Vision Matlab工具箱(此处),其中包括一些梅克斯文件. 在Matlab中,一切都像魅力一样运作,但是当我使用Octave运行代码时,它会抛出此错误:

I am currently porting some code from Matlab to Octave. Some of the functions of the Matlab code use the Piotr's Computer Vision Matlab Toolbox (here) that includes some mex files. In Matlab, everything is working like a charm but when I run my codes with Octave, it throws this error:

error: 'imResampleMex' undefined near line 50 column 5

但是,工具箱中的所有内部路径都应该是正确的.我发现Matlab和Octave处理mex文件的方式是不同的,并试图通过Octave中的C ++函数来构建其中一个mex文件,如下所示:

However all the internal paths within the toolbox should be good. I figured out that the way Matlab and Octave handle mex files is different and tried to build one of the mex files from the C++ function within Octave like this:

mkoctfile --mex imResampleMex.cpp

它失败并抛出与C ++包装函数有关的以下错误消息:

It fails and throws the following error messages related to the C++ wrappers function:

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文件看起来像这样:

The wrappers.hpp file is looking like this:

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

I imagine I need to modify this file but my knowledge of C++ and of mex files being close to non-existent, I am struggling figuring a way out of this mountain of errors. I don't even have a clue whether I'm going in the right direction or not... Any help is welcome!

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

I modified my wrappers.hpp file adding #include <stdlib.h> to it. A mex file is now created. However, when running the function calling the file, I now get the following error:

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

推荐答案

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

Here are the steps I used to solve the issue of creating the mex files from the Piotr's Computer Vision Matlab Toolbox for Octave (concerns the cpp files of the folder channels only).

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

The original mex files that come with the toolbox are built for Matlab and do not work with Octave. While building them from Octave a call is made to the file wrappers.hpp. This file has to be modified by adding these two lines at the beginning of the file: #include <stdlib.h> and #include "mex.h".

要生成mex文件,请在Octave提示符下键入(位于cpp文件的目录中):

For building the mex file, in the Octave prompt type (while being in the directory of the cpp file):

mkoctfile --mex -DMATLAB_MEX_FILE the_file.cpp

通过这种方式可以创建与Octave兼容的mex文件.

This way the Octave compatible mex file is created.

编辑23/05/2017-在收到无法生成这些文件的人的更多问题后,我在Github上发布了生成的文件:

Edit 23/05/2017 - After receiving more questions from people having trouble generating these files I released the generated files on Github: https://github.com/Eskapp/Piotr_vision_extrafiles_Octave. Feel free to use them. You'll need to add them manually to the Computer Vision toolbox.

这篇关于使用Octave构建Mex文件(带有包装的问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆