title: Mex file building with Octave (issue with wrappers)
body: <p>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 (<a href="https://pdollar.github.io/toolbox/" rel="nofollow noreferrer">here</a>) 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:</p>
  
  <pre><code>error: 'imResampleMex' undefined near line 50 column 5
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>mkoctfile --mex imResampleMex.cpp
  </code></pre>
  
  <p>It fails and throws the following error messages related to the C++ wrappers function:</p>
  
  <pre><code>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&amp;, int*&amp;, int*&amp;, T*&amp;, 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&lt;ha+4; y++) C[y]=0;
                                             ^
  warning: mkoctfile: building exited with failure status
  </code></pre>
  
  <p>The wrappers.hpp file is looking like this:</p>
  
  <pre><code>#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) &amp; ~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
  </code></pre>
  
  <p>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!</p>
  
  <p><strong>Edit 1:</strong></p>
  
  <p>I modified my wrappers.hpp file adding <code>#include &lt;stdlib.h&gt;</code> to it. A mex file is now created. However, when running the function calling the file, I now get the following error:</p>
  
  <pre><code>error: failed to install .mex file function 'imResampleMex'
  error: called from
      imResample at line 50 column 4
  </code></pre>