Using Matlab Engine with Eclipse on Linux

We review settings for configuring Eclipse CDT to work with Matlab Engine on Ubuntu Linux.

It can be tricky getting calls to the Matlab Engine to work when using it on Linux and within the Eclipse CDT IDE. This article briefly reviews successful settings for Ubuntu Linux 16.04 (64-bit) using Eclipse Neon CDT (4.6.0) and Matlab R2017A.

For this article, success is considered being able to build the test program provided below, make a call to Matlab's engOpen() with a return pointer to the engine that isn't null, and plot a sine wave.

Assumptions:

  • The reader already has some familiarity with Eclipse CDT and knows how to create and build a new C or C++ project. Note that we are using the CDT builder in this example (not a custom make file).
  • The GNU C/C++ compiler collection is being used to build the code
  • Eclipse Neon CDT (4.6.0) is being used. The settings may be slightly different for other versions.
  • Matlab is already installed. Our installation root directory is /usr/local/MATLAB/R2017a

The settings fall into three categories:

  1. Include path
  2. Libraries
  3. Environment

1. Include Path

Set the include path to the location of "engine.h" in the Matlab installation tree. In our installation, it can be found at /usr/local/MATLAB/R2017a/extern/include..

The Eclipse property settings can be found in Project->Properties->C/C++ Build->Settings.

Setting the Include Path

2. Libraries

We need to specify that both the "eng" and "mx" Matlab libraries should be linked with our executable. We'll also specify inside eclipse the library search path. In our installation, the libraries can be found at /usr/local/MATLAB/R2017a/bin/glnxa64. If you're having difficulty finding the right path, then perform a find from the installation root:

R2017a$ find . -name libmx.so
./bin/glnxa64/libmx.so
Configuring Library Settings

The mx and eng libraries will in turn look for other Matlab libraries, so we'll pass to the gcc linker an rpath directive (run time search path). We use the Xlinker option for this.

Configure Runtime Library Search Path

An alternative to using rpath is to update the library search path in /etc/ld.so.conf. On an Ubuntu installation, we can create a matlab.conf file inside /etc/ld.so.conf.d. Inside this text file, we list the library path. After updating the file, run sudo ldconfig.

3. Environment

You should now be able to compile and link your application with the Matlab engine. However, before you can connect to it successfully, you need to take care of a couple environment issues:

  1. Make sure the matlab executable is in your path. If it's not, then a simple solution is to put a symbolic link to the executable in /usr/bin: "ln -s /usr/local/MATLAB/R2017a/bin/matlab /usr/bin/matlab".
  2. The Matlab engine on Linux also requires a "c shell" to be installed. Confirm that it is installed and on the system path by running "which csh". If you don't have it, then a "sudo apt install csh" should do the trick.

Testing

Run the C application, and you should see the Matlab splash screen followed by the plot shown below.

Matlab Plot

If the code builds but you can't launch Matlab, then make sure you have your PATH configured for both csh and matlab. One way to ensure this is to launch eclipse from your shell:

$ which matlab
/usr/local/MATLAB/R2017a/bin/matlab
$ which csh
/bin/csh
$ eclipse &

Date: May 28, 2012

Author: ahmad

Comment:

Thanks, it is easy, looking for more examples

Date: July 19, 2012

Author: mario

Comment:

I try to configure eclipse to compile and run the example engdemo.cpp, that uses matlab engine. I follow the instructions written here, but I have some errors make all Building target: matlabEngine Invoking: GCC C++ Linker g++ -L/usr/local/MATLAB/R2011a/bin/glnx86 -Xlinker -rpath-link -Xlinker /usr/local/MATLAB/R2011a/bin/glnx86 -o"matlabEngine" ./engdemo.o -leng -lm -lmat -lmex -lut /usr/bin/ld: ./engdemo.o: undefined reference to symbol 'mxDestroyArray' /usr/bin/ld: note: 'mxDestroyArray' is defined in DSO /usr/local/MATLAB/R2011a/bin/glnx86/libmx.so so try adding it to the linker command line /usr/local/MATLAB/R2011a/bin/glnx86/libmx.so: could not read symbols: Invalid operation collect2: ld returned 1 exit status make: * [matlabEngine] Errore 1 When I compile the program from the shell I use these commands, and I have no errors so I can run it. g++ -c -I/usr/local/MATLAB/R2011a/extern/include -I/usr/local/MATLAB/R2011a/simulink/include -DMATLAB_MEX_FILE -ansi -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/local/MATLAB/R2011a/extern/include/cpp -I/usr/local/MATLAB/R2011a/extern/include -DGLNX86 -DGCC -DMX_COMPAT_32 -O -DNDEBUG "engdemo.cpp" g++ -O -o "engdemo" engdemo.o -Wl,-rpath-link,/usr/local/MATLAB/R2011a/bin/glnx86 -L/usr/local/MATLAB/R2011a/bin/glnx86 -leng -lmx -lm But I need to compile in eclipse. Any help? Thanks

Add a new comment here or reply to one above:

email addresses are neither displayed nor shared