Friday, June 14, 2013

Linking C Object to a Mex Executable in GNU Octave

When starting to port the DVB-T2 Common Simulation Platform (CSP from Matlab to Octave, I also started learning about Matlab and Octave.   In the CSP, I saw there were about 3 pieces of code written in C, and I learned that  in Matlab the executables are called Mex (Matlab Executable) files.

GNU Octave provides the same functionality.  You cannot take the compiled Mex files from Matlab and use them with Octave.  A utility call 'mkoctfile' is provided with Octave.  It is actually based on GCC.

So far I have not been able to compile a mex file using a Windows Installer version of Octave.  I have been using Octave in Cygwin, so it is easy to run the mkoctfile command line tool.

e.g.
        mkoctfile --mex myfunc.c

In the CSP, the few mex programs consisted of one C program each.  Here I found one important difference between Matlab and Octave.  In the beginning of a mex program, you will always find:

#include <mex.h>

and mostly, also

#include <matrix.h>

This causes a program in Octave, because mex.h already includes matrix.h.

you can either comment or delete the inclusion of matrix.h, or change it to:

#ifndef HAVE_OCTAVE
#include <matrix.h>
#endif

Yesterday, looking for more LDPC code for Matlab/Octave, I found a package called ISCML at http://code.google.com/p/iscml.  I downloaded the file cml112.zip and found it consist of a large number of C programs.

At the moment I am only interested in the LDPC code, so I found InitState_mx.c and Iterate_mx.c in /module/chan_code/ldpc/decoder/src/mexsrc.   First, I fixed the matrix.h problem as described above. Next, when I tried to compile it, I found it also includes ldpc-util.h and math-supp.h in a deeper directory called lib.  So, standing in the above directory, we can add -Ilib to the command.

Compiling still failed, this time I realised some symbols were not found during the linking phase.  In the lib directory are two more C files, so we can cd into lib and compile them to objects:

mkoctfile -c ldpc-util.c
mkoctfile -c math_supp.c

(Every time you get a of errors, you have to fix the matrix.h problem of course.)

Now cd back to the original directory and compile the two C files:

mkoctfile -c InitState_mx.c -Ilib
mkoctfile -c Iterate_mx.c -Ilib

Now, you link each of the two objects in this directory with the two objects in the lib directory:

mkoctfile --mex Iterate_mx.o lib/ldpc-util.o lib/math-supp.o
mkoctfile --mex InitState_mx.o lib/ldpc-util.o lib/math-supp.o


Now I have two mex files, and have to find out if I can use them!






No comments:

Post a Comment