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!

1

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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?
You also need to set the lib path to where these .lib files exist with the -L option
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.
Because I cannot find any .lib files. The lib folder has almost nothing.
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?
|
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

5 Comments

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."
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: 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..
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: you could have a broken installation, maybe you should reinstall MATLAB
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.

Comments

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.

3 Comments

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: If you don't know what the files are, how are we to?!
Hi Tomalak, I know my questions are naive. I am a beginner and try to find some help here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.