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.
- 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:
- Include path
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.
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
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.
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.
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:
- 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".
- 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.
Run the C application, and you should see the Matlab splash screen followed by the plot shown below.
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 &