Mex file building with Octave (issue with wrappers

2019-03-04 17:36发布

问题:

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

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

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

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

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!

Edit 1:

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

回答1:

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).

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".

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

This way the Octave compatible mex file is created.

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.