Calling matlab from C++
Asked Answered
S

4

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!

Sukkah answered 13/10, 2011 at 16:54 Comment(1)
Check this answer: https://mcmap.net/q/1330410/-engine-matlab-issues/…Haworth
C
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.

Coneflower answered 13/10, 2011 at 17:42 Comment(7)
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?Sukkah
You also need to set the lib path to where these .lib files exist with the -L optionConeflower
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.Sukkah
Because I cannot find any .lib files. The lib folder has almost nothing.Sukkah
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?Coneflower
I tried "find ", and it doesn't find anything. That's why I feel that the installation is not complete.Sukkah
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: mathworks.com/help/matlab/matlab_external/….Ardeb
E
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
Erskine answered 14/10, 2011 at 3:46 Comment(5)
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."Sukkah
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."Sukkah
@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..Erskine
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"Sukkah
@lamothy: you could have a broken installation, maybe you should reinstall MATLABErskine
A
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.

Aught answered 19/1, 2013 at 9:56 Comment(0)
P
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.

Pinguid answered 13/10, 2011 at 16:59 Comment(3)
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?Sukkah
@lamothy: If you don't know what the files are, how are we to?!Your
Hi Tomalak, I know my questions are naive. I am a beginner and try to find some help here.Sukkah

© 2022 - 2024 — McMap. All rights reserved.