Call Matlab from C ++

I tried calling 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 

I got 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 suppose this might be a problem with some links, but I'm not sure. Please help me. Thank you very much in advance!

+6
source share
4 answers

Following what @Kurt S said , you will need to include libraries. These are the usual ones you will need: libeng.lib libmat.lib libmx.lib, but you may need others.

So you want to add layout options -llibeng -llibmat -llibmx

But you may need others.

+2
source

Here is a simple make file to get you 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 

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


You can also compile this directly from within MATLAB. First, make sure you mbuild -setup at least once:

 >> srcFile = fullfile(matlabroot,'extern','examples','eng_mat','engdemo.cpp'); >> mbuild(srcFile, '-llibeng','-llibmx') >> !engdemo 
+2
source

The problem is the incorrect specification of the included files and folders (i.e. for libraries and link files) and several additional dependencies.

You can use a simple demo code for the C / C ++ interface, and MATLAB is here to understand what you need to do.

You also need to use the CMAKELISTS.TXT file with the appropriate settings for MATLAB, for which a good tutorial is available here .

+1
source

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

0
source

Source: https://habr.com/ru/post/899243/


All Articles