1

I'm trying to compile a c++ program which contains Matlab's provided engine header. File MLP.cpp contains:

#include <engine.h>
#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;

And references the matlab functions highlighted in the errors below. When running:

g++ -c MLP.cpp -I/usr/local/matlab/extern/include -L/usr/local/matlab/extern/lib -llibeng -llibmx -lmatlab
g++ MLP.o -o main

I get the following errors:

MLP.o: In function `MatLabPredictor::MatLabPredictor(char*)': 

MLP.cpp:(.text+0x18): undefined reference to `engOpen'

MLP.cpp:(.text+0x36): undefined reference to `engEvalString'

MLP.cpp:(.text+0x4a): undefined reference to `engEvalString'

MLP.cpp:(.text+0x5e): undefined reference to `mxCreateDoubleMatrix'

MLP.cpp:(.text+0x79): undefined reference to `mxGetPr'

MLP.o: In function `MatLabPredictor::~MatLabPredictor()':

MLP.cpp:(.text+0xa1): undefined reference to `engClose'

MLP.o: In function `MatLabPredictor::retrain(double)':

MLP.cpp:(.text+0x104): undefined reference to `engPutVariable'

MLP.cpp:(.text+0x118): undefined reference to `engEvalString'

MLP.cpp:(.text+0x12c): undefined reference to `engEvalString'

MLP.cpp:(.text+0x140): undefined reference to `engEvalString'

MLP.o: In function `MatLabPredictor::predict_next_value()':

MLP.cpp:(.text+0x162): undefined reference to `engEvalString'

MLP.cpp:(.text+0x176): undefined reference to `engGetVariable'

MLP.cpp:(.text+0x186): undefined reference to `mxGetData'

MLP.cpp:(.text+0x199): undefined reference to `mxDestroyArray'
collect2: ld returned 1 exit status

I have also tried changing the compilation commands to:

g++ -c MLP.cpp -I/usr/local/matlab/extern/include -L/usr/local/matlab/bin/glnxa64 -llibeng -llibmx -lmatlab
g++ MLP.o -o main
Amro
  • 123,847
  • 25
  • 243
  • 454
  • Try adding "-v" to see if g++ actually links the libraries. The error messages clearly say it does not link libmx and libeng. If you have a "libmx.so" file somewhere, a `-lmx` is usually the proper way to specify it for linking. – sebastian Nov 14 '13 at 07:58

3 Answers3

1

First g++ command you specify is for compiling, you need only -I option for that. Give it path to folder where engine.h is (-I$MATLABROOT/extern/include - Let's say MATLABROOT points to root directory of Matlab installation, in this case /usr/local/matlab).

Second g++ command is for linking, you need to put -L and -l(s) there. Something like -L$MATLABROOT/bin/glnxa64 -leng -lmx

So we end up with this sequence:

g++ -c MLP.cpp -I$MATLABROOT/extern/include

g++ MLP.o -o main -L$MATLABROOT/bin/glnxa64 -leng -lmx

For getting the same, but in a single line:

g++ MLP.c -o main -I$MATLABROOT/extern/include -L$MATLABROOT/bin/glnxa64 -leng -lmx

Note: libeng.so and libmx.so must be reachable when you want to run this executable, so extend LD_LIBRARY_PATH or PATH with folder: $MATLABROOT/bin/glnxa64 before attempting to run main.

durasm
  • 174
  • 3
  • 10
0

On 64-bit Linux, you probably want change the lib path to:

${MATLABROOT}/extern/lib/glnxa64

chappjc
  • 30,359
  • 6
  • 75
  • 132
0

The easiest method to compile engine programs is to use the mex command along with the supplied options file engopts.sh:

>> engopts = fullfile(matlabroot,'bin','engopts.sh');
>> mex('-f',engopts, 'MLP.cpp')

If you want you could run the above with the verbose flag mex -v ..., and copy the generated compilation and linking flags into your own build system.

(I believe the problem is that you should drop the lib part from the library names: g++ file.cpp -I${MROOT}/extern/include -L${MROOT}/extern/lib/${ARCH} -leng -lmx)

Note: dont forget to set the LD_LIBRARY_PATH so that your program is able to find the required MATLAB shared libraries when run.

See these pages for more info.

Amro
  • 123,847
  • 25
  • 243
  • 454
  • It cannot find -leng and -lmx ... I'm not sure about LD_LIBRARY_PATH ... is it a directory for leng and lmx? – user2989812 Nov 14 '13 at 21:25
  • @user2989812 There should be a glnxa64 subdirectory (${ARCH} in Amro's answer) in extern/lib. Be sure you are pointing there with -L. Can you confirm this? Worry about the runtime libraries (LD_LIBRARY_PATH) after you have a successful link. – chappjc Nov 14 '13 at 22:07
  • @user2989812: did you use the `engopts` file as I described? `mex` will know how to compile engine programs, you wont have have to worry about the compilation/linking process.. Just make sure to run `mex -setup` at least once before – Amro Nov 14 '13 at 23:38
  • g++ MLP.cpp -I/usr/local/matlab/extern/include -L/usr/local/matlab/extern/lib/glnxa64 -leng -lmx : /usr/bin/ld: cannot find -leng /usr/bin/ld: cannot find -lmx collect2: ld returned 1 exit status This the error I'm getting doing the intructions – user2989812 Nov 15 '13 at 01:04