0

I am trying to configure eclipse to compile and run the example engdemo.cpp, that uses the matlab engine.

I followed the instructions written here, but I have still 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?

Rob
  • 4,927
  • 12
  • 49
  • 54

1 Answers1

0

Usually linker errors can be quite nasty to debug. In this case the error is very informative.

undefined reference to symbol 'mxDestroyArray' indicates that a library containing the symbol called mxDestroyArray is not being linked in. However, it does tell you that the library is called libmx.so and is found in /usr/local/MATLAB/R2011a/bin/glnx86 so that you know where to look.

In your g++ -O ... command, you are including the path to this library (... -L/usr/local/MATLAB/R2011a/bin/glnx86 ...), which explains why it works.

You also need to reference the library itself in the linker command using the -l flag, seen by the -lmx in the g++ -O ... command.

You need to do the same thing in Eclipse.

In Eclipse, you can find the project paths and symbols by selecting the Project menu and going to Properties. Expand C/C++ General and select Paths and Symbols. This dialog allows you to tell Eclipse where the libraries the linker needs are located, similar to the -L parameter in g++.

To do so, add the library name and path to the lists under the Library Paths and Libraries tabs.

NOTE: Adding the path is not enough, you also need to specify the library name (in g++ it's the -l parameter, in this case, -lmx)

On the Libraries tab, add a new library called mx. This should add a -lmx to your eclipse linker command.

stanri
  • 2,922
  • 3
  • 25
  • 43
  • Yes, I have already done this. Infact in the eclipse console the command that it uses shows that the path it's correct: 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 – user1537705 Jul 19 '12 at 12:56
  • yeah, you've included the library path, but not the actual library (identified by the `-lmx`), which is not found in the eclipse build command. I've added a note to my answer to explain this in more detail. – stanri Jul 19 '12 at 13:12
  • Thanks! Now the code compiles and links. But when I try to run it says matlab: Command not found. The matlab command is in my search path infact I can call it from my shell. My here in eclipse console it can't found this command. How do I do to set up it? Thanks for the support – user1537705 Jul 19 '12 at 14:06