从 C++ 调用 matlab

发布于 2024-12-09 20:02:39 字数 698 浏览 0 评论 0原文

我尝试从 .cpp 文件调用 matlab。我使用以下命令来编译 engdemo.cpp 其中包括 "engine.h"

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

我得到的是以下内容:

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 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清风无影 2024-12-16 20:02:39

按照@Kurt S所说的,您需要包含库。这些是您需要的常见库:libeng.lib libmat.lib libmx.lib,但您可能还需要其他库。

因此,您想要添加链接选项 -llibeng -llibmat -llibmx

但您可能还需要其他选项。

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.

花伊自在美 2024-12-16 20:02:39

这里有一个简单的 makefile 可帮助您入门:

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

只需通过调用 make 使用它,然后以 ./engdemo 运行程序


即可。您也可以直接从 MATLAB 内部编译它。首先确保您至少运行过一次 mbuild -setup 命令:

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

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
人疚 2024-12-16 20:02:39

问题是包含文件和文件夹(即库和链接文件)以及一些附加依赖项的指定不正确。

您可以使用一个简单的演示代码来连接 C/C++ 和 MATLAB 这里,以便了解需要做什么。

此外,您还需要使用具有适合 MATLAB 设置的 CMAKELISTS.TXT 文件,对此有一个很好的教程 这里

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.

可可 2024-12-16 20:02:39

您需要使用 g++ 的 -l 选项告诉它要链接哪些库。根据您的链接行,该库应位于 /usr/local/matlabR2010a/extern/lib 中。例如,如果您需要的库名为 libmatlab.a,您需要将 -lmatlab 选项添加到命令行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文