从 C++ 调用 matlab
我尝试从 .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 compileengdemo.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
按照@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.
这里有一个简单的 makefile 可帮助您入门:
Makefile
只需通过调用
make
使用它,然后以./engdemo
运行程序即可。您也可以直接从 MATLAB 内部编译它。首先确保您至少运行过一次 mbuild -setup 命令:
Here is a simple makefile to help you get started:
Makefile
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:问题是包含文件和文件夹(即库和链接文件)以及一些附加依赖项的指定不正确。
您可以使用一个简单的演示代码来连接 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.
您需要使用 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.