6

I tried to call matlab from a .cpp file. I used the following command to compile engdemo.cpp which includes "engine.h"

g++ engdemo.cpp -I/usr/local/matlabR2010a/extern/include -L/usr/local/matlabR2010a/extern/lib -o engdemo

What I got is the following:

engdemo.cpp:(.text+0xdb): undefined reference to `engOpen'
engdemo.cpp:(.text+0x12d): undefined reference to `mxCreateDoubleMatrix'
engdemo.cpp:(.text+0x143): undefined reference to `mxGetPr'
engdemo.cpp:(.text+0x175): undefined reference to `engPutVariable'
engdemo.cpp:(.text+0x189): undefined reference to `engEvalString'

...

collect2: ld returned 1 exit status


I guess it might be some link problem but I am not sure. Please help me out. Many thanks in advance!

Amro
  • 123,847
  • 25
  • 243
  • 454
Lamothy
  • 337
  • 4
  • 17

4 Answers4

2

Following up on what @Kurt S said, you'll need to include libraries. These are common ones you'll need: libeng.lib libmat.lib libmx.lib, but you might need others.

Thus you want to add the linking options -llibeng -llibmat -llibmx

But you might need others as well.

Community
  • 1
  • 1
Chris A.
  • 6,817
  • 2
  • 25
  • 43
  • Thank you for your reply. I tried all these three linking options, and I got all similar results /usr/bin/ld: cannot find -llibeng collect2: ld returned 1 exit status How can I find all the linking options? – Lamothy Oct 13 '11 at 17:58
  • You also need to set the lib path to where these .lib files exist with the `-L` option – Chris A. Oct 13 '11 at 18:01
  • Thanks. I guess the matlab I installed was probably incomplete. I need to download all the missing packages first. Thank you so much for your help. – Lamothy Oct 13 '11 at 18:41
  • Because I cannot find any .lib files. The lib folder has almost nothing. – Lamothy Oct 13 '11 at 21:18
  • That's surprising. Did you do a search for `libeng.lib` with something like the search option in Windows or with `find -name libeng.lib` in Unix/Linux? – Chris A. Oct 13 '11 at 21:32
  • I tried "find ", and it doesn't find anything. That's why I feel that the installation is not complete. – Lamothy Oct 13 '11 at 22:36
  • This is a really old thread, but for other people with this problem, on a Linux OS, you should be searching for libeng.so, not libeng.lib. See this link: http://www.mathworks.com/help/matlab/matlab_external/compiling-engine-applications-in-an-ide.html. – vrume21 Aug 18 '15 at 18:08
2

Here is a simple makefile to help you get started:

Makefile

# root directory of MATLAB installation
MATLABROOT="/usr/local/matlabR2010a"

all: engdemo

engdemo:
    g++ ${MATLABROOT}/extern/examples/eng_mat/engdemo.cpp -o engdemo \
        -I${MATLABROOT}/extern/include \
        -L${MATLABROOT}/extern/lib -llibeng -llibmx

clean:
    rm -f engdemo *.o

Simply use it by calling make, then running the program as ./engdemo


You can also compile this directly from inside MATLAB. First make sure you have run mbuild -setup command at least once:

>> srcFile = fullfile(matlabroot,'extern','examples','eng_mat','engdemo.cpp');
>> mbuild(srcFile, '-llibeng','-llibmx')
>> !engdemo
Amro
  • 123,847
  • 25
  • 243
  • 454
  • Amro, thank you very much for your post. I tried both ways, while it seems there are still problems. For the first one, calling make, I got this "makefile:11: *** missing separator. Stop." – Lamothy Oct 14 '11 at 13:45
  • For the second, it says "/usr/local/matlabR2010a/extern/examples/eng_mat/engdemo.cpp: In function ‘int main()’: /usr/local/matlabR2010a/extern/examples/eng_mat/engdemo.cpp:113: warning: ignoring return value of ‘char* fgets(char*, int, FILE*)’, declared with attribute warn_unused_result /usr/bin/ld: cannot find -llibeng collect2: ld returned 1 exit status mbuild: link of 'engdemo' failed. ??? Error using ==> mbuild at 193 Unable to complete successfully." – Lamothy Oct 14 '11 at 13:48
  • @lamothy: Makefiles require a leading tab (`\t`) not spaces (` `) to start the action clause. If you copy/pasted from the above, SO might have formatted all tabs as spaces.. – Amro Oct 14 '11 at 17:19
  • I see. You are definitely right. Now what I got is the following "-I"/usr/local/matlabR2010a"/extern/include \ -L"/usr/local/matlabR2010a"/extern/lib -llibeng -llibmx /usr/bin/ld: cannot find -llibeng collect2: ld returned 1 exit status make: *** [engdemo] Error 1" – Lamothy Oct 15 '11 at 01:17
  • @lamothy: you could have a broken installation, maybe you should reinstall MATLAB – Amro Oct 15 '11 at 01:25
1

The problem is improper specification of include files and folders (i.e. for libraries and link files) and a few additional dependencies.

You can make use of a simple demo code for the interfacing C/C++ and MATLAB is given here, so as to understand what needs to be done.

Also you need to use a CMAKELISTS.TXT file with suitable settings for MATLAB, for which a good tutorial is available here.

Preeti Kaur
  • 419
  • 4
  • 5
0

You need to tell it which libraries to link against with the -l option to g++. Based on your link line, the library should be in /usr/local/matlabR2010a/extern/lib. As an example, if the library you need is called libmatlab.a you need to to add the -lmatlab option to the command line.

Kurt Stutsman
  • 3,994
  • 17
  • 23
  • 1
    Thank you so much for your reply. I'm sorry that I'm not sure the name of the library. In the /usr/local/matlabR2010a/extern/lib folder, I found a several files ended with .map. For example MexLibrary.map. What should I add to the command line? – Lamothy Oct 13 '11 at 17:47
  • 1
    @lamothy: If you don't know what the files are, how are we to?! – Lightness Races in Orbit Oct 13 '11 at 17:58
  • Hi Tomalak, I know my questions are naive. I am a beginner and try to find some help here. – Lamothy Oct 13 '11 at 18:12