Calling matlab from C++

前端 未结 4 588
北荒
北荒 2020-12-10 07:23

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

g++ engde         


        
相关标签:
4条回答
  • 2020-12-10 07:57

    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.

    0 讨论(0)
  • 2020-12-10 08:00

    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.

    0 讨论(0)
  • 2020-12-10 08:13

    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
    
    0 讨论(0)
  • 2020-12-10 08:16

    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.

    0 讨论(0)
提交回复
热议问题