title: How to call Matlab from C++ code?
body: <p>I am trying to call Matlab functions from C++ code.</p>
  
  <p>With Matlab it comes an example of such code at /extern/examples/eng_mat/engdemo.cpp, however I found no way to build that source code.</p>
  
  <p>Here is the makefile I use:</p>
  
  <pre><code>CFLAGS = -Wall -O3
  
  INCLUDES = -I/opt/Matlab-2009a/extern/include
  
  LIBRARIES = -Wl,-R/opt/Matlab-2009a/bin/glnx86 -L/opt/Matlab-2009a/bin/glnx86 -lmx -lmat -leng
  
  out : engdemo.cpp
      g++ $(CFLAGS) $(INCLUDES) -static $^ $(LIBRARIES) -o out
  
  clean :
      rm -f out
  </code></pre>
  
  <p>(Here /opt/Matlab-2009a is my Matlab root.) I am getting a linker error like this:</p>
  
  <pre><code>/usr/bin/ld: cannot find -lmx
  collect2: ld returned 1 exit status
  make: *** [out] Error 1
  </code></pre>
  
  <p>And the question is: how can I make g++ to compile engdemo.cpp ?</p>
  
  <p>Note, that the shared library exists:</p>
  
  <pre><code>$ locate libmx.so
  /opt/Matlab-2009a/bin/glnx86/libmx.so
  /opt/Matlab-2009a/bin/glnx86/libmx.so.csf
  </code></pre>
  
  <p>and</p>
  
  <pre><code>$ ldd /opt/Matlab-2009a/bin/glnx86/libmx.so
      linux-gate.so.1 =&gt;  (0x004b4000)
      libut.so =&gt; /opt/Matlab-2009a/bin/glnx86/../../bin/glnx86/libut.so (0x0078f000)
      libmwfl.so =&gt; /opt/Matlab-2009a/bin/glnx86/../../bin/glnx86/libmwfl.so (0x00110000)
      libicudata.so.38 =&gt; /opt/Matlab-2009a/bin/glnx86/../../bin/glnx86/libicudata.so.38 (0xb7f82000)
      libicuuc.so.38 =&gt; /opt/Matlab-2009a/bin/glnx86/../../bin/glnx86/libicuuc.so.38 (0x00bee000)
      libicui18n.so.38 =&gt; /opt/Matlab-2009a/bin/glnx86/../../bin/glnx86/libicui18n.so.38 (0x001f7000)
      libicuio.so.38 =&gt; /opt/Matlab-2009a/bin/glnx86/../../bin/glnx86/libicuio.so.38 (0x00e1c000)
      libz.so.1 =&gt; /usr/lib/libz.so.1 (0x0098e000)
      libstdc++.so.6 =&gt; /opt/Matlab-2009a/bin/glnx86/../../sys/os/glnx86/libstdc++.so.6 (0x00531000)
      libm.so.6 =&gt; /lib/libm.so.6 (0x00194000)
      libgcc_s.so.1 =&gt; /opt/Matlab-2009a/bin/glnx86/../../sys/os/glnx86/libgcc_s.so.1 (0x00eaa000)
      libpthread.so.0 =&gt; /lib/libpthread.so.0 (0x00900000)
      libc.so.6 =&gt; /lib/libc.so.6 (0x00345000)
      librt.so.1 =&gt; /lib/librt.so.1 (0x00964000)
      libdl.so.2 =&gt; /lib/libdl.so.2 (0x0014e000)
      libexpat.so.1 =&gt; /opt/Matlab-2009a/bin/glnx86/../../bin/glnx86/../../bin/glnx86/libexpat.so.1 (0x00152000)
      libboost_thread-gcc42-mt-1_36.so.1.36.0 =&gt; /opt/Matlab-2009a/bin/glnx86/../../bin/glnx86/../../bin/glnx86/libboost_thread-gcc42-mt-1_36.so.1.36.0 (0x00fc2000)
      libboost_signals-gcc42-mt-1_36.so.1.36.0 =&gt; /opt/Matlab-2009a/bin/glnx86/../../bin/glnx86/../../bin/glnx86/libboost_signals-gcc42-mt-1_36.so.1.36.0 (0x0017d000)
      libboost_system-gcc42-mt-1_36.so.1.36.0 =&gt; /opt/Matlab-2009a/bin/glnx86/../../bin/glnx86/../../bin/glnx86/libboost_system-gcc42-mt-1_36.so.1.36.0 (0x00a06000)
      /lib/ld-linux.so.2 (0x001db000)
  </code></pre>
  
  <p>So, how can I make g++ to compile engdemo.cpp ?</p>