How to create a Mex project using Linux Eclipse

By | June 25, 2016

This method for creating Matlab Mex executable file has been tested using Matlab R2010b & Eclipse 3.7.1 & 64 bit Fedora 16 Linux system.

0. Compile Mex in Matlab command prompt

For your information, C/C++ Mex file can be compiled in Matlab command prompt using ‘mex’, for instance,

mex –g –v GNUMex.c

The OpenMP support can be turned on by adding compiling and linker flag ‘-fopenmp’, such as

mex –g -v GNUMexOpenMP.c CFLAGS="\$CFLAGS -fopenmp" LDFLAGS="\$LDFLAGS -fopenmp"


mex –g -v GNUMexOpenMP.cpp CXXFLAGS="\$CXXFLAGS -fopenmp" LDCXXFLAGS="\$LDCXXFLAGS -fopenmp"

 1. Creation of an empty project

  • Create a simple C or C++ project under Eclipse (File / New/ C Project), name it as you wish


  • Choose Empty Project under Shared Library, follow project wizard until an empty C or C++ project is created


2. Configuration of Compiler

Now we need to define some important properties of the project to ensure that the output file is fully compatible with Matlab. Open project properties (Right click on the project name / Properties). Start with the “ C/C++ Build” tab

  • Under “Settings/Tool Settings/Cross GCC Compiler/Symbols” add ‘MATLAB_MEX_FILE’ as a defined symbols


  • Under “Settings/Tool Settings/Cross GCC Compiler/Includes” add Matlab include folder which is usually /home/Username/MATLAB/R2010b/extern/include


  • Under “Settings/Tool Settings/Cross GCC Linker/Libraries” add Matlab library folder which is usually /home/Username/MATLAB/R2010b/bin/glnxa64 for 64bit Linux system, this path may vary according to your system, however, do not use /home/Username/MATLAB/R2010b/extern/lib


  • Under “Settings/Tool Settings/Cross GCC Linker/Libraries” also add Matlab library file, and into Libraries section, since lots of default Mex functions are linked to these libraries. Note here mat, mx, and mex are added, this will be fine otherwise missing function error will appear.


  • We also need to configure the output file, since the default output file extension is not compatible with Matlab. Under “Settings/Build Artifact” change Artifact extension as ‘mexa64’ for 64bit Linux or ‘mexa32’ for 32bit, Output prefix can be left as blank.


3. Configuration for OpenMP

If OpenMP is used in the source code, this step is necessary, otherwise ignore this part.

  • Open features for OpenMP by adding ‘-fopenmp’ under ‘Command’ boxes for both ‘Cross GCC Compiler’ and ‘Cross GCC Linker’ (not shown here).


  • Also check the ‘-fPIC’ flag for successful compiling, this option is under ‘Cross GCC Compiler/Miscellaneous’.


4. mexFunction

Add a main C/C++ file that contains the body of “mexFunction” with the following code snippet ( do not forget to include the “mex.h” header file to give access to the prototype of the mexFunction), of course, any needed header files has to be put at the front of source file, such as

#include "mex.h"
#include <stdio.h>
#include <omp.h>
void mexFunction(int nlhs,mxArray* plhs[], int nrhs, const mxArray* prhs[])
   int count;
   int th_id;
   #pragma omp parallel for private(count, th_id) num_threads(2)
   for( count = 0; count < 60; count++ )
      th_id = omp_get_thread_num();
      printf("Hello World from thread %d, %d\n", th_id, count);
   printf( "Finished\n" );

5. Building

Now if you build the project (Right click / “Build”) you will get a mexw32 file (or mexw64) which can be executed in Matlab.

6. Conclusion

After all the steps, you will be able to compile your Mex file in Eclipse as what I did. Some online tutorials require adding module-definition file (.def) and ‘mexversion.rc’. However, Matlab removed ‘mexversion.rc’ in recent releases (?). I have tested compiling mex file under Visual Studio 2008 without this file and it turned out just fine. Unsurprisingly it also turned out fine for compiling Mex without this file under linux.  I also tried to add .def file as mentioned online, But I always got the error “unrecognized option –Wl, –output-def = XXX”. After trying solutions for hours, I finally decided to drop this file and it worked just fine. As a conclusion, I would recommend to compile using default Maltab Mex script for simple Mex project. So take your own risk to compile this way and enjoy the power of Mex.

Also check “How to create a Mex project using Visual Studio 2008” for compiling Mex in Windows.